Q3: Reverse a Singly Linked List

Reverse a singly linked list in-place by reassigning the next pointers using three iteration references: prev, curr, and next.

Iterative Linked List Reversal
function reverseList(head) {
  let prev = null;
  let curr = head;
  while (curr !== null) {
    let nextTemp = curr.next;
    curr.next = prev;
    prev = curr;
    curr = nextTemp;
  }
  return prev;
}

At each step, we save curr.next, point curr.next backward to prev, advance prev to curr, and move curr to the saved nextTemp pointer.

Key Takeaways
  • Reverses pointers in linear O(n) time and O(1) auxiliary space.

  • Requires storing curr.next in a temporary pointer before updating node link.

Knowledge Check

Which reference becomes the new head of the reversed linked list upon loop completion?