#Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = NoneclassSolution(object):defreorderList(self,head):""" :type head: ListNode :rtype: void Do not return anything, modify head in-place instead. """ifhead==Noneorhead.next==None:return# split the listprevNode=ListNode(-1)prevNode.next=headslow=prevNodefast=prevNodewhilefastandfast.next:slow=slow.nextfast=fast.next.nextright=slow.nextslow.next=Noneleft=head# reverse the rightprev=rightnextNode=right.nextprev.next=NonewhilenextNode!=None:tmp=nextNode.nextnextNode.next=prevprev=nextNodenextNode=tmpright=prev# Merge the listwhileright!=None:leftNext=left.nextrightNext=right.nextleft.next=rightright.next=leftNextleft=leftNextright=rightNext