# Definition for singly-linked list. # class ListNode: # def init(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reorderList(self, head: ListNode) -> None: """ Do not return anything, modify head in-place instead. """ if not head: return [] # Find the middle of the list slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next slow.next,mid = None,slow.next # del last, mid=last node # Reverse the half after middle dummyHead = ListNode(0) while mid: print(mid.val) dummyHead.next, mid.next, mid = mid, dummyHead.next, mid.next # Start reorder one by one reverse = dummyHead.next while reverse: head.next,reverse.next,reverse,head = reverse,head.next,reverse.next,head.next