Skip to content
intermediate

Reverse a linked list in place

Complete the three-pointer reversal — the previous, the current, and the next that keeps the rest alive.

interface ListNode {
  value: number
  next?: ListNode
}

/**
 * Returns the same nodes, last to first. The input list is rewired in place
 * and also returned; `head` itself is no longer the head afterwards.
 */
export function reverseList(head: ListNode | undefined): ListNode | undefined {
  let ________: ListNode | undefined = undefined
  let _______: ListNode | undefined = head

  while (current !== undefined) {
    // Hold on to the rest before turning the pointer around.
    const next: ListNode | undefined = current.next
    current.next = previous
    previous = current
    current = next
  }

  return previous
}
0/2 filled tab moves · Ctrl+↵ submits · Ctrl+Shift+↵ runsstarter code