easy Reverse a LinkedList
Problem Statement
Given the head of a Singly LinkedList, reverse the LinkedList. Write a function to return the new head of the reversed LinkedList.
Constraints:
- The number of nodes in the list is the range
[0, 5000]. -5000 <= Node.val <= 5000
Try it yourself
Try solving this question here:
🎯 STRICT STANDOUT — Reverse a Linked List (easy)
1. Why / judgment
Iterative: prev=null, cur=head; while cur: nxt=cur.next; cur.next=prev; prev=cur; cur=nxt; return prev. Recursion: reverse rest then head.next.next=head. Pattern foundation for reverse-k-group, palindrome list. When-NOT: need original preserved -> copy first.
2. Big-O derivation (K11)
O(n) time; iterative O(1) space; recursive O(n) stack.
1-2-3-null becomes 3-2-1
Empty/single fixed points
3. Pattern + when-NOT (K12)
Name: LINKED LIST REVERSAL
Recognition: reverse singly linked list pointers.
When-NOT: Doubly list reverse swap next/prev. Reverse print only -> stack/recursion without mutating.
4. Edge hand-run (K13)
two nodes swap
cycle input undefined — assume acyclic
5. Interviewer follow-ups (model answers)
Q1. Why save nxt?
A: Before rewiring next, else lose rest of list.
Q2. Recursive base?
A: null or single node returns itself.
Q3. k-group relation?
A: Reverse sublist utility + pointer join.
6. Short drills
Drill: 1-2
Drill: draw 3-pointer table
Drill: reverse between left right indices
✅ Solution Reverse a LinkedList
Problem Statement
Given the head of a Singly LinkedList, reverse the LinkedList. Write a function to return the new head of the reversed LinkedList.
Constraints:
- The number of nodes in the list is the range
[0, 5000]. -5000 <= Node.val <= 5000
Solution
To reverse a LinkedList, we need to reverse one node at a time. We will start with a variable current which will initially point to the head of the LinkedList and a variable previous which will point to the previous node that we have processed; initially previous will point to null.
In a stepwise manner, we will reverse the current node by pointing it to the previous before moving on to the next node. Also, we will update the previous to always point to the previous node that we have processed. Here is the visual representation of our algorithm:
Code
Here is what our algorithm will look like:
/*class ListNode {
int val = 0;
ListNode next;
ListNode(int val) {
this.val = val;
}
}*/
class Solution {
public ListNode reverse(ListNode head) {
ListNode current = head; // current node that we will be processing
ListNode previous = null; // previous node that we have processed
ListNode next = null; // will be used to temporarily store the next node
while (current != null) {
next = current.next; // temporarily store the next node
current.next = previous; // reverse the current node
// before we move to the next node, point previous to the current node
previous = current;
current = next; // move on the next node
}
// after the loop current will be pointing to 'null' and 'previous' will be the
// new head
return previous;
}
public static void main(String[] args) {
Solution sol = new Solution();
ListNode head = new ListNode(2);
head.next = new ListNode(4);
head.next.next = new ListNode(6);
head.next.next.next = new ListNode(8);
head.next.next.next.next = new ListNode(10);
ListNode result = sol.reverse(head);
System.out.print("Nodes of the reversed LinkedList are: ");
while (result != null) {
System.out.print(result.val + " ");
result = result.next;
}
}
}
Time Complexity
The time complexity of our algorithm will be O(N) where ‘N’ is the total number of nodes in the LinkedList.
Space Complexity
We only used constant space, therefore, the space complexity of our algorithm is O(1).
🎯 STRICT STANDOUT — Solution Reverse a Linked List
1. Why / judgment
Three-pointer iterative reverse as standard. Table for 1-2-3: each step rewires one edge. Final prev is new head. Test empty and single. Recursive alternative for completeness; prefer iterative in production depth limits.
2. Big-O derivation (K11)
O(n)/O(1) iterative
Edges reversed exactly n times
No lost nodes if nxt saved
3. Pattern + when-NOT (K12)
Name: IN-PLACE LIST REVERSE
Recognition: reverse singly list.
When-NOT: Reverse print only. Reverse data array copy.
Reversal as a reusable sub-routine. The same three-pointer move is the core of several harder problems — recognition: any time you must flip next-pointers in place:
- Reverse Nodes in k-Group (LC25) — reverse each block of k nodes.
- Reverse Linked List II (LC92) — reverse the sublist between positions [m, n].
- Palindrome Linked List (LC234) / Reorder List (LC143) — reverse the second half, then compare/merge.
Recursive variant (reverses 1 → 2 → 3 into 3 → 2 → 1):
ListNode reverse(ListNode head) {
if (head == null || head.next == null) return head; // base: empty or last node
ListNode p = reverse(head.next); // p is the new head (deepest node)
head.next.next = head; // make the next node point back to head
head.next = null; // head becomes the tail
return p;
}
Correct, but it uses O(n) stack space (one frame per node), versus O(1) for the iterative version — which is why the iterative form is preferred under recursion-depth limits (n up to 5000 here).
4. Edge hand-run (K13)
2-node list 1 → 2 → null, per-step pointer trace (columns: cur, prev, nxt, list so far):
| Step | cur | prev | nxt | list state |
|---|---|---|---|---|
| start | 1 | null | — | 1 → 2 → null |
| after iter 1 (nxt=2; 1.next=null; prev=1; cur=2) | 2 | 1 | 2 | 1 → null (prev chain) |
| after iter 2 (nxt=null; 2.next=1; prev=2; cur=null) | null | 2 | null | 2 → 1 → null |
Loop exits when cur == null; return prev = 2, the new head of 2 → 1 → null.
Empty list: head == null makes the while (current != null) body never run, so previous is still null and we return null — the correct reversal of an empty list.
5. Interviewer follow-ups (model answers)
Q1. Common bug?
A: Forgetting nxt=cur.next before cur.next=prev.
Q2. Return value?
A: prev not original head.
Q3. Stack reverse values?
A: O(n) space; not pure pointer reverse.
6. Short drills
Drill: 1-2-3-4-5
Drill: recursive reverse
Drill: reverse first k nodes only
Recognize it: In-place pointer surgery → a dummy head + fast/slow pointers.
▶ Visualize this problem (step it, predict each fork)
🤖 Don't fully get this? Learn it with Claude
Stuck on Reverse a LinkedList? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Reverse a LinkedList** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Reverse a LinkedList** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Reverse a LinkedList**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Reverse a LinkedList**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.