Description
Reorder linked list by interleaving from start and end.
Constraints
- Time Complexity:
O(n) - Space Complexity:
O(1)
Tags
linked-listtwo-pointersrecursion
def reorderList(head):
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
second = slow.next
prev = slow.next = None
while second:
tmp = second.next
second.next = prev
prev = second
second = tmp
first, second = head, prev
while second:
tmp1, tmp2 = first.next, second.next
first.next = second
second.next = tmp1
first, second = tmp1, tmp2