Free beta: 30 days of full access, no card needed.Sign up free

We use necessary cookies to run the site (sign-in and language). The contact and bug-report forms also use Google reCAPTCHA for spam protection, loaded only if you accept. Privacy policy

Field manual

Linked list reversal

O(n)

Walk the list once, and at each node flip its .next pointer to point backward instead of forward, carrying prev/curr/next along so nothing gets lost.

Signals

reverse a linked list (whole list or between positions)reverse in groups of kno extra array or O(1) extra space allowedswap pairs of nodessingly linked list, only .next available

Template

function reverseList(head) {
    let prev = null;
    let curr = head;
    while (curr) {
        const next = curr.next;
        curr.next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

Looks similar, isn't

  • Two pointers (same direction): Same-direction pointers walk indices over an array, one reading fast and one writing slow. Reversal has no array and no indices; it rewires the .next field on each node in place, which is pointer surgery, not index walking.

n up to 1e5 nodes -> O(n): one pass touching each node's pointer once, O(1) extra space.

Learn this pattern