In-Place Linked List Reversal

The prev/curr/next pointer dance — reverse a whole list, a sub-list, or every k-group in O(1) extra space.

Intermediate 2 min readLesson 10 of 25

Reversing a linked list in place means re-pointing each node's next to its previous node, using three pointers and no extra memory. It's the single most common linked-list manipulation and a building block for palindrome checks, reordering and k-group reversal.

InteractiveReverse a linked list in place
1
→ 2
curr
2
→ 3
3
→ 4
4
→ 5
5
→ null
prevcurr
prev = null, curr = head
1/12
When to use this pattern
  • "Reverse a linked list" — fully, between positions m and n, or in groups of k.
  • Problems that need to walk backwards on a singly linked list (palindrome check, adding numbers stored forwards).
  • Constraint: O(1) extra space.

Reverse the whole list

Hold three references: prev (already-reversed part), curr (node being processed) and next (saved before we overwrite curr.next).

ListNode reverse(ListNode head) {
    ListNode prev = null, curr = head;
    while (curr != null) {
        ListNode next = curr.next;   // 1. save
        curr.next = prev;            // 2. reverse the pointer
        prev = curr;                 // 3. advance prev
        curr = next;                 // 4. advance curr
    }
    return prev;                     // new head
}

// Recursive version: O(n) stack space
ListNode reverseRec(ListNode head) {
    if (head == null || head.next == null) return head;
    ListNode newHead = reverseRec(head.next);
    head.next.next = head;
    head.next = null;
    return newHead;
}

Reverse a sub-list (positions left..right)

Walk to the node before position left (use a dummy head in case left = 1), then repeatedly move the node after the sub-list's original first node to the front of the sub-list ("head insertion").

ListNode reverseBetween(ListNode head, int left, int right) {
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode before = dummy;
    for (int i = 1; i < left; i++) before = before.next;
    ListNode tail = before.next;                 // becomes the tail of the reversed part
    for (int i = 0; i < right - left; i++) {
        ListNode move = tail.next;               // node to move to the front
        tail.next = move.next;
        move.next = before.next;
        before.next = move;
    }
    return dummy.next;
}

Reverse nodes in k-groups

Check that k nodes remain; if so, reverse exactly those k, connect the previous group's tail to the new group head, and continue. Leftover nodes (fewer than k) stay as they are.

ListNode reverseKGroup(ListNode head, int k) {
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode groupPrev = dummy;
    while (true) {
        ListNode kth = groupPrev;
        for (int i = 0; i < k && kth != null; i++) kth = kth.next;
        if (kth == null) break;                          // fewer than k left
        ListNode groupNext = kth.next;
        ListNode prev = groupNext, curr = groupPrev.next;
        while (curr != groupNext) {                      // standard reversal
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        ListNode oldFirst = groupPrev.next;              // now the group's tail
        groupPrev.next = kth;                            // kth is the new group head
        groupPrev = oldFirst;
    }
    return dummy.next;
}
Practice problems
  • Reverse Linked Listeasy
  • Palindrome Linked Listreverse second halfeasy
  • Reverse Linked List IImedium
  • Swap Nodes in Pairsk-group with k = 2medium
  • Rotate Listmake a ring, then cutmedium
  • Reorder Listmedium
  • Add Two Numbers IIreverse, add, reversemedium
  • Reverse Nodes in k-Grouphard
Key takeaways
  • Save next, reverse the pointer, advance prev, advance curr — in that order.
  • A dummy node makes reversals starting at the head uniform.
  • Sub-list reversal: walk to the node before the range, then head-insert.
  • All variants are O(n) time and O(1) space.