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
Reversing a Singly Linked List (1 → 2 → 3 → 4 → 5) The Three-Pointer Sliding Window Mechanism Initial Problem: Forward-only access (No back-pointers!) val: 1 val: 2 val: 3 val: 4 val: 5 null If cur.next = null Nodes 2-5 are lost! The Sliding Window In Action: prev (null) → cur (1) → nxt (2) prev cur nxt null prev val: 1 cur flipped val: 2 nxt val: 3 val: 4 val: 5 null Core Loop Steps (Repeated until cur === null): 1. let nxt = cur.next Save rest of chain 2. cur.next = prev Flip pointer backward 3. prev = cur Slide prev forward 4. cur = nxt Slide cur forward
Reverse 1→2→3→4→5 overview diagram
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.
python
# Given head of 1 -> 2 -> 3 -> 4 -> 5 -> null
# Goal: Reverse in place and return the new head (5 -> 4 -> 3 -> 2 -> 1 -> null)

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
Why You Need Pointer Reversal in Singly Linked Lists Overcoming forward-only traversal with three pointer choreography (prev, curr, next) The Trap: Severing the Path If 1.next points to null first, nodes 2→5 are instantly orphaned (no back-pointers exist!) Node 1 next: null ❌ Path Lost! Node 2 Node 3 Node 4 Node 5 → null The Solution: Three-Pointer Choreography (prev, curr, next) Save next → Reverse curr.next → Advance prev & curr Node 1 val: 1 | next: • Node 2 val: 2 | next: • Node 3 val: 3 | next: • … Node 5 val: 5 | next: null prev Initially null, trails behind curr curr Active processing node next Saves lifeline to remaining list flip pointer
Why You Need Pointer Reversal in Singly Linked Lists diagram
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.
Reversing a Singly Linked List: The Three-Pointer Sliding Window Example: 1 → 2 → 3 → 4 → 5 (Mid-iteration snapshot at Node 3) prev (points backward) cur (active node) nxt (stash remainder) Node 1 val: 1 prev Node 2 val: 2 Node 3 val: 3 cur Node 4 val: 4 nxt Node 5 val: 5 null flipped cur.next 1 Stash Remainder Before breaking the forward link, save the rest of the chain safely in pointer: nxt = cur.next (Node 4) 2 Flip the Pointer Redirect current node's next reference backward to point to previous: cur.next = prev 3 Slide Window Forward Advance both working pointers one step right to prepare for next loop: prev = cur, cur = nxt
The Three-Pointer Sliding Window Mental Model diagram
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

javascript
function ListNode(val, next) {
    this.val = (val===undefined ? 0 : val)
    this.next = (next===undefined ? null : next)
}

function reverseList(head) {
    let prev = null;
    let cur = head;
    while (cur !== null) {
        let nxt = cur.next; // Must save next first!
        cur.next = prev;    // Flip the pointer
        prev = cur;         // Shift prev forward
        cur = nxt;          // Shift cur forward
    }
    return prev;
}
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.
javascript
function reverseList(head) {
    let prev = null;
    let cur = head;
    // TODO: complete the while loop for 1->2->3->4->5
}
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 = null
Steps (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 cur is null.
- We return prev, which now anchors the completely reversed list starting at 5 (5 → 4 → 3 → 2 → 1 → null).
javascript
function reverseList(head) {
    let prev = null;
    let cur = head;
    while (cur !== null) {
        let nxt = cur.next;
        cur.next = prev;
        prev = cur;
        cur = nxt;
    }
    return prev;
}
Phase 4: Tracing the Three-Pointer Sliding Window (1 → 2 → 3 → 4 → 5) Active Loop Trace: nxt = cur.next → cur.next = prev → prev = cur → cur = nxt (Repeats until cur == null) prev (null) cur (head) nxt (temp) Sliding across nodes 1 through 5, reversing pointer directions in-place. Node 1 val: 1 prev (It 1) next Node 2 val: 2 cur (It 1) next Node 3 val: 3 nxt (save) next Node 4 val: 4 next Node 5 val: 5 null How Iterations Progress & Loop Termination: • Iteration 1-5: Sliding window advances step-by-step. Each node's .next pointer is flipped backward to point to prev. • Termination: Loop exits when cur becomes null. prev now anchors the newly reversed list starting at node 5. • Final Output: Returns prev → resulting in 5 → 4 → 3 → 2 → 1 → null.
Tracing the Full Reversal of 1→2→3→4→5 diagram
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.
javascript
class ListNode {
    constructor(val = 0, next = null) {
        this.val = val;
        this.next = next;
    }
}

function reverseList(head) {
    // TODO: Initialize prev, cur, and nxt pointers
    // TODO: Loop through the nodes, reversing links
    // TODO: Return the 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.
javascript
function isPalindrome(head) {
  // 1. Find middle
  // 2. Reverse second half using the prev/cur/nxt pattern
  // 3. Compare first and second half
  // 4. Return boolean
}

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.

Keep learning