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 LinkedList easy
0. Why / judgment (K3)
Family: Pointer rewiring · iterative reverse
Reverse next pointers iteratively with prev/curr/next_tmp triad. Recursive reverse is elegant but O(n) stack. Empty and single-node are no-ops returning head.
1. Pattern + recognition + when-NOT (K12)
Pattern / template: prev=null; curr=head; while curr: nxt=curr.next; curr.next=prev; prev=curr; curr=nxt; return prev. Recognition: reverse linear structure links.
When-NOT: Not convert to array unless allowed. Not reverse in pairs (different problem). Recursive if stack limit tight → iterative.
2. Complexity derivation (K11)
Time Θ(n), space Θ(1) iterative / Θ(n) recursive stack.
3. Edge hand-run (K13)
[] → [].
[1] → [1].
[1,2,3]: steps → 1←2←3, prev=3.
Cycles: problem assumes acyclic; Floyd first if not.
4. Interviewer follow-ups & drills
Q1. New head?
Model answer: Former tail = prev at end.
Q2. Why save nxt?
Model answer: Rewire loses forward link otherwise.
Q3. Reverse k-group?
Model answer: Use same block reverse as subroutine.
✅ 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 LinkedList
0. Why / judgment (K3)
Family: Iterative pointer reverse
Code quality: clear three-pointer loop; no dummy needed. Draw boxes in interview. Recursive: reverse(rest) then head.next.next=head; head.next=null.
1. Pattern + recognition + when-NOT (K12)
Pattern / template: Iterative triad or recursive reverse.
When-NOT: Don't iterate with extra list of nodes if O(1) space required.
2. Complexity derivation (K11)
Θ(n)/Θ(1) iterative.
3. Edge hand-run (K13)
Hand-run 1→2→3→null:
curr1: nxt2; 1→null; prev1 curr2
curr2: nxt3; 2→1; prev2 curr3
curr3: nxtnull; 3→2; prev3 currnull → head 3.
4. Interviewer follow-ups & drills
Q1. Recursive base?
Model answer: head is null or head.next null return head.
Q2. Tail pointer after?
Model answer: Old head becomes tail with next null.
Q3. Hostile interviewer: prove no leak
Model answer: Every next reassigned exactly once to prev chain.
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.