"""Definition of ListNodeclass ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next"""classSolution:""" @param head: The first node of the linked list. @return: nothing """defreorderList(self,head):# write your code here# split the list from the middle# reverse the right sublist# merge the two listsifhead==Noneorhead.next==None:returnhead# find the pivotdummy=ListNode(0,None)fast_node=dummypivot=dummydummy.next=headwhilefast_node!=Noneandfast_node.next!=None:fast_node=fast_node.next.nextpivot=pivot.nextright_list=pivot.next# terminate the left listpivot.next=Noneleft_list=head# reverse the right listprev_node=right_listnext_node=right_list.next# terminate the right listprev_node.next=Nonewhilenext_node!=None:tmp_point=next_node.nextnext_node.next=prev_nodeprev_node=next_nodenext_node=tmp_pointright_list=prev_node# merge right_list and left_listdummy.next=left_listwhileleft_list!=Noneandright_list!=None:next_left=left_list.nextleft_list.next=right_listnext_right=right_list.nextright_list.next=next_leftleft_list=next_leftright_list=next_rightreturndummy.next