LeetCode 143 Reorder List - Implementation

The approach

Find middle, reverse second half, merge alternating.

function reorderList(head): if not head or not head.next: return slow = head fast = head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next second = slow.next slow.next = null second = reverse(second) first = head while second: tmp1 = first.next tmp2 = second.next first.next = second second.next = tmp1 first = tmp1 second = tmp2

O(n)O(n) time, O(1)O(1) space.