Walk the list with three references: prev, cursor, and next. Each iteration saves cursor.next, re-points cursor.next backward to prev, then advances prev = cursor; cursor = next.

Algorithm

Basic Implementation

basic.ts
class ListNode {
    value: number;
    next: ListNode | null;
    constructor(value: number, next: ListNode | null) {
        this.value = value;
        this.next = next;
    }
}

const n5: ListNode = new ListNode(5, null);
const n4: ListNode = new ListNode(4, n5);
const n3: ListNode = new ListNode(3, n4);
const n2: ListNode = new ListNode(2, n3);
let head: ListNode | null = new ListNode(1, n2);

let prev: ListNode | null = null;
let cursor: ListNode | null = head;
while (cursor !== null) {
    const next: ListNode | null = cursor.next;
    cursor.next = prev;
    prev = cursor;
    cursor = next;
}
head = prev;

The three-pointer loop saves the forward link, flips one next pointer, then advances prev and cursor.

Step 1 - Save the first forward link

prev starts at null, cursor is node(1), and nxt saves node(2).

Initial 1 -> 2 -> 3 -> 4 -> 5 chain with prev, cursor, and nxt named.prevcursornxtnullnode(1)node(2)node(3)node(4)node(5)

Step 2 - Flip node(1)

Set node(1).next to prev, making the reversed prefix 1 -> null.

After the first flip, prev points at node(1) and cursor advances to node(2).prevcursornode(1)nullnode(2)node(3)node(4)node(5)

Step 3 - Reversed prefix reaches 3

After three flips, the prefix is 3 -> 2 -> 1 -> null and cursor is node(4).

Middle of the reverse: prefix 3 -> 2 -> 1, suffix 4 -> 5.prevcursornode(3)node(2)node(1)nullnode(4)node(5)

Step 4 - Done

When cursor reaches null, prev is the new head: 5 -> 4 -> 3 -> 2 -> 1 -> null.

Final reversed list.headnode(5)node(4)node(3)node(2)node(1)null

Complexity

  • Time: O(n)
  • Space: O(1)

Implementation notes

  • TypeScript: same three-pointer pattern as the other languages. Each pointer carries the ListNode | null union type so the end-of-list null is honest.
  • Reverse in place and reassign head = prev at the end.
  • The replay shows all three pointers each frame and a distinct rewire frame between save and advance.
three pointers `prev` starts `null`, `cursor` starts at `head`, `next` is the saved forward link.
rewire The rewire frame flips `cursor.next` from forward (toward `next`) to backward (toward `prev`).