beginner9 min read·Updated September 22, 2026
Reverse a Singly Linked List Explained: Reversing 1→2→3→4→5
Master pointer reversal for 1→2→3→4→5 with a 3-pointer mental model. Step-by-step trace, code implementation, and complexity breakdown included.
By Learnisim AI·Published September 22, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
- basic pointer or reference concepts
- understanding of singly linked list node structures
Why
Why You Need Pointer Reversal in Singly Linked Lists
Phase 1: Why Reversing a Linked List Breaks Intuition
Imagine you are handed the head of a singly linked list:
1 -> 2 -> 3 -> 4 -> 5 -> null. Your goal is to rearrange these nodes in place so that the sequence becomes 5 -> 4 -> 3 -> 2 -> 1 -> null. Unlike a standard array where you can instantly swap elements using indices like arr[0] and arr[4], a singly linked list forces you to move only in one direction—forward.If you start at node
1, you can easily see its neighbor 2, but node 1 has no idea what node 0 was because back-pointers do not exist. If you naively change 1.next to point to null to start building your reversed list, you instantly sever your path to nodes 2, 3, 4, and 5. You are stranded.This structural limitation is why reversing a linked list requires a deliberate choreography of temporary variables. You cannot simply overwrite pointers as you encounter them without losing the rest of your data structure.
Model
The Three-Pointer Sliding Window Mental Model
Phase 2: The Three-Pointer Sliding Window
When reversing our working example of 1 2 3 4 5 null, we cannot simply walk forward because changing 1.next to point to null instantly severs our connection to nodes 2, 3, 4, and 5. To prevent this memory leak, we use a sliding window of three pointers: prev, cur, and nxt.
Before we break any forward arrow, we stash the remainder of the chain in nxt. Then, we flip cur.next to point backward toward prev. Finally, we slide all three pointers one step forward down the track. This choreography repeats until cur runs off the end of the chain, leaving prev resting squarely on our new head at 5.
Syntax
Syntax and Node Structure for Reversing 1→2→3→4→5
Now that we visualize our three-pointer sliding window, how do we write it down in code? To reverse our locked example 1→2→3→4→5, we need a standard
ListNode class definition and a function signature that accepts head. The syntax relies on basic reference assignment: saving next pointers before we overwrite them, shifting prev to cur, and advancing cur to nxt.Phase 3: The Code Skeleton
A classic syntax trap on this structure is forgetting to declare
nxt = cur.next inside the loop. If you write cur.next = prev before capturing cur.next, you instantly orphan nodes 2→3→4→5 and your traversal stops dead at node 1.Worked example
Tracing the Full Reversal of 1→2→3→4→5
Phase 4: Worked Example
Now we watch the three-pointer sliding window in action. We are given the head of our canonical list:
1 → 2 → 3 → 4 → 5 → null. Our goal is to reverse the pointers in place and return the new head pointing to 5.Given / Setup:
-
-
-
-
prev = null-
cur = 1-
nxt = nullSteps (Iteration Trace):
1.Iteration 1 (cur at 1):
-
nxt = cur.next (saves 2)-
cur.next = prev (flips node 1 to point to null)-
prev = cur (prev moves to 1)-
cur = nxt (cur moves to 2)- State:
prev at 1, cur at 2. List segment: 1 → null, rest is 2 → 3 → 4 → 5 → null.2.Iteration 2 (cur at 2):
-
nxt = cur.next (saves 3)-
cur.next = prev (flips node 2 to point to 1)-
prev = cur (prev moves to 2)-
cur = nxt (cur moves to 3)- State:
prev at 2, cur at 3. List segment: 2 → 1 → null.3.Iteration 3 (cur at 3):
-
nxt = cur.next (saves 4)-
cur.next = prev (flips node 3 to point to 2)-
prev = cur (prev moves to 3)-
cur = nxt (cur moves to 4)- State:
prev at 3, cur at 4.4.Iteration 4 (cur at 4):
-
nxt = cur.next (saves 5)-
cur.next = prev (flips node 4 to point to 3)-
prev = cur (prev moves to 4)-
cur = nxt (cur moves to 5)- State:
prev at 4, cur at 5.5.Iteration 5 (cur at 5):
-
nxt = cur.next (saves null)-
cur.next = prev (flips node 5 to point to 4)-
prev = cur (prev moves to 5)-
cur = nxt (cur becomes null, terminating the loop)Result:
- The loop terminates because
- We return
- The loop terminates because
cur is null.- We return
prev, which now anchors the completely reversed list starting at 5 (5 → 4 → 3 → 2 → 1 → null).Practice
Write and Test Your Own Reversal Function
Phase 5: Practice
You have traced every pointer update for our working example of 1→2→3→4→5, watching how
prev, cur, and nxt transform the forward links into a backward chain. Now it is time to put your code into action and build the function yourself. Using the standard ListNode structure, complete the reverseList function so that it successfully transforms our working list into 5→4→3→2→1→null.Take your time setting up your three local variables before entering the loop. Remember to handle the traversal step-by-step just like we did in the trace: save the next node, redirect the current pointer backward, shift
prev forward, and advance cur to your saved reference. When the loop terminates, ensure you return the correct new head.Apply
Applying Pointer Reversal to Palindrome Linked List Checks
Phase 6: Transferring the Pointer Reversal Pattern
Now that you have mastered reversing our original 1→2→3→4→5 list, let us see how this exact three-pointer mechanism solves a completely different problem: determining if a linked list is a palindrome. In a palindrome list like 1→2→3→2→1, the sequence reads the same forwards and backwards. Naively copying all node values into an array requires extra space. But we can solve this in auxiliary space by finding the middle of the list, reversing the second half using our exact reversal loop, and then comparing the front and back halves node by node.
The Transfer Strategy
To check if our list is a palindrome without extra memory, follow these steps:
1.Find the middle node of the list using slow and fast pointers.
2. Apply the exact same pointer reversal logic (, , ) to reverse the second half of the list starting from the middle.
3.Compare the nodes of the first half and the reversed second half sequentially.
4.(Optional best practice) Reverse the second half back to restore the original list structure.
Notice how the local pointer manipulation technique you learned for 1→2→3→4→5 becomes a reusable building block for structural algorithms.
FAQ
How do you reverse the specific list 1→2→3→4→5?
You use three pointers—prev, curr, and next—to iteratively redirect each node's next pointer backward. Starting with curr at 1, you repeatedly store curr.next, point curr.next to prev, advance prev and curr, and finally land on 5→4→3→2→1→null.
What is the time and space complexity of reversing a singly linked list?
The time complexity is O(n) because you traverse each of the n nodes exactly once. The space complexity is O(1) for the iterative approach since it only requires a few pointer variables regardless of the list size.
What happens if you try to reverse an empty list or a list with a single node?
If the head is null (empty list) or points to a single node with a null next pointer, the algorithm immediately terminates or returns the head as-is, which is already correctly reversed.
Why do we need a temporary 'next' pointer during reversal?
Without saving the next node in a temporary variable before changing curr.next to prev, you would sever the connection to the rest of the list and lose access to the remaining unreversed nodes.