Reverse Linked List

easy

Given the head of a singly linked list, reverse the list and return the head of the reversed list.

A singly linked list is a chain of nodes where each node holds a value and a single next pointer to the node that follows it. Reversing the list means the node that was last becomes the new head, and every next pointer is flipped to point at what used to come before it.

You are given only the head pointer — there are no prev pointers and no size field. Return the head of the list after the direction of every link has been reversed.

Hints

You can only hold the head, and you must flip every arrow to point the other way. What do you risk losing the moment you flip one?
Before you overwrite a node's next pointer, you need to remember where it used to point — otherwise the rest of the list is gone.
Carry three pointers as you walk once: the reversed part so far (prev), the node you're flipping (curr), and the saved rest (nxt).

Common doubts

No. The optimal solution reverses the existing links in place, so no new node is allocated and you use only O(1) extra space.
Return null (or the language's nil/None). With head empty, the loop never runs and prev stays null, which is exactly correct — no special case needed.
Because curr.next = prev overwrites the only reference to the rest of the list. Saving nxt = curr.next first preserves your path forward.

Interview follow-ups

Yes — recurse to the end, then on the way back set head.next.next = head and head.next = null. It's elegant but uses O(n) stack space, which can overflow on very long lists — mention that trade-off.
Walk to position m-1, then apply the same three-pointer flip n-m times to reverse just that segment, and carefully reconnect the segment's ends to the surrounding list.

Fun facts

  • The three-pointer 'flip as you walk' pattern is the backbone of almost every in-place linked-list problem — reverse in k-groups, swap pairs, and palindrome checks all reuse it.
  • Reversing a list is secretly a stack: the copy-to-array approach makes the stack explicit, while the pointer version builds the reversed chain implicitly one link at a time.

Asked at

AmazonMicrosoftGoogleAdobeMetaApple
Frequently Sometimes Occasionally
Example 1
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
Every next pointer is flipped, so the tail 5 becomes the new head.
Example 2
Input: head = [1,2]
Output: [2,1]
Example 3
Input: head = []
Output: []
An empty list reversed is still empty.
Constraints

- The number of nodes in the list is in the range [0, 5000]. - -5000 <= Node.val <= 5000

Solve this problem →