Reorder List Medium 0 attempts
LeetCode ↗

Reorder List

Medium Linked List LeetCode

Reorder a linked list from L0→L1→...→Ln to L0→Ln→L1→Ln-1→L2→Ln-2→...

Example: head = [1,2,3,4] → [1,4,2,3]

Sample Input
Sample Output
Constraints
  • 1 to 5 * 10^4 nodes
  • 1 <= Node.val <= 1000
Topics

Find Middle + Reverse + Merge

function reorderList(head) {
  let slow = head, fast = head;
  while (fast.next && fast.next.next) { slow = slow.next; fast = fast.next.next; }
  let prev = null, curr = slow.next;
  slow.next = null;
  while (curr) { const next = curr.next; curr.next = prev; prev = curr; curr = next; }
  let l1 = head, l2 = prev;
  while (l2) { const n1 = l1.next, n2 = l2.next; l1.next = l2; l2.next = n1; l1 = n1; l2 = n2; }
}

Time: O(n) | Space: O(1)

Saved in this browser only. Private to you.

JavaScript