What Are the Three Pointers in a Linked-List Reversal?

Reversing a linked list keeps previous, current, and the next node you are about to lose. You save next before you overwrite current.next.

A linked list node holds a value and next. Reversing it rewires those links. You keep three names because one of them exists only to remember a node you are about to disconnect.

How it works

previous starts as null. current starts at the head. Before you change current.next, copy it into next. Then point current.next at previous. Step forward: previous becomes current, and current becomes the saved next.

When current is null, previous is the new head. The old head's next is null, which is what you wanted. The walk is O(n) and uses O(1) extra memory beyond the list you were given.

Losing the saved next is the bug. Once you overwrite current.next, the rest of the list is unreachable unless you kept that reference.

Theme
Terminal

Press Run to execute this example. Use console.log to print a value.

Expected output: [3, 2, 1]. On each iteration, saved still points to the unreversed suffix while previous points to the reversed prefix.

Worked example

Initially the split is null | 1 -> 2 -> 3. After one rewire it is 1 -> null | 2 -> 3; after two it is 2 -> 1 -> null | 3. The final rewire leaves 3 -> 2 -> 1 -> null, and previous points to 3.

Why it is correct

At the top of each iteration, previous heads a correctly reversed prefix and current heads the untouched suffix. Saving current.next preserves access to that suffix; redirecting the link adds exactly one node to the reversed prefix. Advancing both names restores the invariant. When the suffix is empty, the reversed prefix contains every original node, so previous is the new head.

Cost

Each of n nodes is visited and rewired once, giving O(n) time. The three references use O(1) extra space. This mutates the list; building a separate reversed list would preserve the input but require O(n) new nodes.

In the course

Linked lists introduce the three names and then ask you to reverse. The following lesson uses two pointers instead: one with a head start for the nth node from the end, and a slow pointer with a fast pointer for the middle. Same list, different jobs.

Common mistakes

Overwriting next before you have saved it, and then walking off into null.

Returning current at the end. current is null. previous is the new head.

Reversing the array the tests used to build the list. The starter already turned that array into nodes. The rewire is the exercise.

When to use it

Use the three names whenever you rewire a singly linked list in one pass and still need the remainder of the list.

When not to

Do not reach for three pointers when a second pointer with a head start answers the question. The middle and the nth-from-the-end walks are that simpler pair.

Problem-solving checklist

Draw the reversed prefix and unreversed suffix separated by a bar. Save the suffix before changing a link. Test null, one node, and two nodes. Confirm that the old head ends with next: null and that you return previous, not current.

Optional enrichment

Reverse only the first k nodes and reconnect the untouched suffix. Define the behaviour for k = 0), k` larger than the list, and an empty list before coding.