Reading preferences
Concept

In-Place Linked List Reversal

Reverse links while preserving the unvisited remainder.

python, linked lists, reversal, pointers

Reversing a chain changes which node each link points to. Before turning a link backward, save the next node so the remainder is still reachable.

Python illustration

class Node:
    def __init__(self, value, next_node=None):
        self.value = value
        self.next = next_node

def reverse(head):
    previous = None
    current = head
    while current is not None:
        following = current.next
        current.next = previous
        previous = current
        current = following
    return previous

head = Node("A", Node("B", Node("C")))
head = reverse(head)
while head is not None:
    print(head.value)
    head = head.next

Prepared output: C, B, A on separate lines.

See the reasoning

Reverse and preserve the remainder

Follow the reversed prefix and unvisited suffix. This is a prepared demonstration, not live code execution.

Start

Linked chains
  1. Reversed prefixNone
  2. RemainingA → B → C → NoneCurrent focus

No link has been reversed.

Preserving both parts

At each step previous heads the reversed prefix and current heads the remaining original suffix. Saving following preserves the suffix before the link is reversed. At the end the returned node heads the whole reversed chain. For a finite acyclic list of n nodes, time is O(n) and extra space O(1). The operation mutates links: aliases to nodes observe the change. It is not suitable for a cyclic list without additional handling.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. Why save the next node before changing the link?
2. What does previous refer to during the loop?
3. What happens for an empty list?
4. What extra space is required?

References and further reading