Try it first — give it 15 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
You get head, the first node of a singly linked list: a chain of nodes where each node holds a value val and one arrow next pointing to the node after it. The last node's next is null.
Reverse the list and return the new first node. The list can be empty (head is null) and has at most 5,000 nodes.
Example: 1 → 2 → 3 → 4 → 5 becomes 5 → 4 → 3 → 2 → 1, and you return the node holding 5.
Start with brute force
Copy every value into an array, then walk the list again and write the values back in reverse order. Or push every node onto a stack and pop them off, relinking as you go.
Both are correct and O(n) time. Both also use O(n) extra memory for the array or stack. An interviewer will accept it as a first step, then ask: "Can you do it with O(1) extra space?" That's the real question.
Building the intuition
Draw the list on paper as boxes and arrows:
null 1 → 2 → 3 → nullWhat does the answer look like? null ← 1 ← 2 ← 3. Same boxes, same places. Only the arrows changed direction. So you don't need new nodes. You need to visit each node once and turn its arrow around.
Try it on node 1. Its arrow should point at whatever came before it. Nothing came before it, so it should point at null. Write 1.next = null.
Now you're stuck. That arrow was your only way to reach 2. You just cut the rope you were climbing.
That tells you two things:
- Before you flip an arrow, save where it pointed. Call it
nxt. - You need to remember the node behind you, because that's what the flipped arrow should point at. Call it
prev. For node1,previsnull.
Now each step is the same three moves: save, flip, step. Repeat until you run out of nodes.
The optimal approach
- Set
prev = nullandcurr = head.previs the head of the reversed part, which starts empty. - While
currisn'tnull:- Save:
nxt = curr.next. - Flip:
curr.next = prev. - Step:
prev = curr, thencurr = nxt.
- Save:
- Return
prev.
The subtle detail is what you return. The loop ends when curr is null, one step past the old last node. The old last node is in prev, and it's the new head. Returning head is a classic bug: head is now the tail, and you'd return a one-node list.
The order inside the loop matters too. prev = curr must come before curr = nxt. Swap them, and prev ends up pointing at the wrong node.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None # head of the part we've already reversed (empty at the start)
curr = head # the next node whose arrow we need to flip
while curr:
nxt = curr.next # 1. save the rest of the list, or we lose it when we flip
curr.next = prev # 2. flip: point this node back at the reversed part
prev = curr # 3. step both pointers forward:
curr = nxt # curr joins the reversed part, and we move on to the saved rest
return prev # curr fell off the end, so prev is the old tail, now the new headclass Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr; // head of the part we've already reversed (empty at the start)
ListNode* curr = head; // the next node whose arrow we need to flip
while (curr != nullptr) {
ListNode* nxt = curr->next; // 1. save the rest, or we lose it when we flip
curr->next = prev; // 2. flip: point this node back at the reversed part
prev = curr; // 3. step both pointers forward
curr = nxt;
}
return prev; // curr fell off the end, so prev is the old tail, now the new head
}
};class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null; // head of the part we've already reversed (empty at the start)
ListNode curr = head; // the next node whose arrow we need to flip
while (curr != null) {
ListNode nxt = curr.next; // 1. save the rest, or we lose it when we flip
curr.next = prev; // 2. flip: point this node back at the reversed part
prev = curr; // 3. step both pointers forward
curr = nxt;
}
return prev; // curr fell off the end, so prev is the old tail, now the new head
}
}/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function (head) {
let prev = null; // head of the part we've already reversed (empty at the start)
let curr = head; // the next node whose arrow we need to flip
while (curr !== null) {
const nxt = curr.next; // 1. save the rest, or we lose it when we flip
curr.next = prev; // 2. flip: point this node back at the reversed part
prev = curr; // 3. step both pointers forward
curr = nxt;
}
return prev; // curr fell off the end, so prev is the old tail, now the new head
};Dry run
head = 1 → 2 → 3. The "list state" column shows every arrow after that step.
| Step | nxt saved | Flip | prev after | curr after | List state |
|---|---|---|---|---|---|
| start | – | – | null | 1 | 1 → 2 → 3 → null |
| 1 | 2 | 1.next = null | 1 | 2 | null ← 1 and 2 → 3 → null |
| 2 | 3 | 2.next = 1 | 2 | 3 | null ← 1 ← 2 and 3 → null |
| 3 | null | 3.next = 2 | 3 | null | null ← 1 ← 2 ← 3 |
curr is null, so the loop stops. Return prev, the node 3: the list reads 3 → 2 → 1.
Notice that in the middle of the loop the list is in two pieces: the reversed part starting at prev, and the untouched part starting at curr. nxt is the thread that keeps the second piece reachable.
Complexity
- Time: O(n). Each node is visited once, and each visit does three pointer assignments.
- Space: O(1). Three pointer variables, however long the list is.
Edge cases and traps
- Empty list.
currstarts asnull, the loop never runs, and you returnprev = null. Correct, with no special case. - One node. One loop iteration sets its
nexttonulland returns it. - Returning
head. After the loop,headis the last node. Returnprev. - Flipping before saving.
curr.next = prevfirst loses the rest of the list forever. - Wrong step order.
curr = nxtthenprev = currmakesprevandcurrthe same node.
Say it in the interview
"I don't need new nodes; I just need to flip every
nextpointer. I'll keepprev, the head of the reversed part, starting at null, andcurr. At each node I savecurr.next, pointcurr.nextback atprev, then moveprevandcurrforward one step. Whencurrbecomes null,previs the new head. That's O(n) time and O(1) space."
Follow-ups interviewers ask
- "Can you do it recursively?" Reverse the rest first (
newHead = reverseList(head.next)), then sethead.next.next = headandhead.next = null, and returnnewHead. It's O(n) time but O(n) stack space, and 5,000 levels can be risky in some languages. - "Reverse only positions
lefttoright." That's Reverse Linked List II. Walk to the node beforeleft(a dummy head handlesleft = 1), reverse that stretch with the same three moves, then reconnect both ends. - "Where else is this used?" Constantly. Reorder List later in this stage reverses the second half of a list as one of its steps.
Practise the same idea
This is Day 43 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free