Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Linked Lists

In-place Reversal

O(n) / O(1)

Re-point next pointers one node at a time while carrying prev/curr; for k-groups, reverse a chunk then stitch.

  1. 1Learnthe lesson(you are here)
  2. 2Guidedwatch it run, line by line(not yet)
  3. 3Practisethe path, or the climb(not yet)
  4. 4Revisereviews on schedule(not yet)
Start the lesson
Learn firstFast & Slow Pointers
0%
mastery
0/11 solved
IdeaPracticeTemplatePitfalls

Recognise it

The signal

Reverse a whole list, a sublist [left, right], or every k nodes; reorder, rotate, swap pairs, palindrome check, in O(1) space.

  1. 1Brute force

    Copy values to an array, manipulate, write back.

  2. 2The observation

    O(n) extra space and copying; pointer surgery does it in place.

  3. 3Why it works

    prev heads the already-reversed part, curr heads the untouched part; save next before re-pointing so nothing is dropped.

Reach for it when

  • +Reverse a whole list, a sublist [left,right], or every k nodes — in O(1) space.
  • +Rearrange a list by reversing part of it — reorder list, palindrome check, swap pairs.
  • +Any pointer-rewiring where recursion's O(n) stack is unacceptable.

Not when

  • ×You need random access or to sort by valueuse Copy to an array
  • ×You only need to read values in reverse, not rewire, and O(n) space is fineuse A stack of values
  • ×It's really cycle detection or finding the middle (often a step before reversing)use Fast & slow pointers
  • ×The list is immutable or shared with other codeuse Don't rewire it; build a copy
  • ×You think 'dump to an array and reverse': O(n) space and can't splice a segment back inuse Rewire .next in place
  • ×You think opposite-end swapping works: it needs O(1) indexing, which lists lackuse Iterative prev/curr/next

Intuition

Reversing a linked list means flipping every next pointer to point backward, using O(1) extra space (no array copy). The trick is a three-pointer dance: prev, curr, and a saved next.

Read the full intuition

The loop, every iteration:

  1. Save nxt = curr.next (or you lose the rest of the list).
  2. Flip curr.next = prev (point this node backward).
  3. Slide prev = curr, then curr = nxt.

When curr falls off the end, prev is the new head. Three lines of pointer-shuffling that you should be able to write in your sleep — it's the building block for reversing a sublist, swapping pairs, reversing in k-groups, and rearranging lists (reorder, palindrome).

Key idea

The hard variants all reduce to "find the boundary nodes, reverse the middle, then re-stitch the ends." Use a dummy head node so the case where the reversal includes the real head stops being a special case.

Why it costs what it costs

Time O(n). each node's pointer is rewired exactly once.

Space O(1). iterative version uses three pointers; recursion (k-group) adds O(n/k) or O(n) stack.

Practice

Next · Foundation

Reverse Linked List

prev/curr/next loop; also write the recursive version.

0/11 solved
  • Reverse Linked ListFoundationFoundationAmazon, MetaEasy
  • Reverse Linked List IIApply it · Reverse only positions left..right.Apply itMedium
  • Swap Nodes in PairsOne change · Reverse every group of 2.One changeAmazon, AdobeMedium
  • Reverse Nodes in k-GroupTwists · Groups of k; the last incomplete group stays as is.TwistsAmazon, MicrosoftHard
  • Rotate ListBranches · Rotation instead of reversal.BranchesAmazon, AdobeMedium
  • Reorder ListCombine · Reversal is one step in a pipeline.CombineAmazon, MetaMedium
  • Odd Even Linked ListAmazon, AdobeMedium
  • Remove Duplicates from Sorted ListAmazon, Morgan StanleyEasy
  • Add Two NumbersAmazon, AdobeMedium
  • Copy List with Random PointerAmazon, MetaMedium
  • Palindrome Linked ListAmazon, AdobeEasy

Pitfalls

Classic bugs

  • !Lost the rest of the list: save nxt = curr.next before reassigning curr.next.
  • !No dummy head, so reversing a range that includes the real head becomes a buggy special case.
  • !k-group: reverse a group only if k full nodes remain; leave the trailing partial group as-is.
  • !After reversing a middle segment, re-stitch both ends: node before → new sub-head, old sub-head → node after.
  • !Recursive reversal uses O(n) stack and can overflow on long lists; use the iterative form.
Go deepervariants and techniques · 1 combinations · interviewer follow-ups · resources

Variants and techniques

  • Full reverseprev/curr/next loop; return prev. Reverse Linked List.
  • Sublist reverseDummy + 'before' pointer, hoist nodes to the front of the segment. Reverse Linked List II.
  • k-group reverseReverse each full block of k, recurse/iterate the rest, stitch. Reverse Nodes in k-Group, Swap Nodes in Pairs (k=2).
  • Reverse half + interleaveFind middle (fast/slow), reverse second half, merge. Reorder List, Palindrome Linked List.
  • Rotate / splitFind length, reconnect into a ring, cut at the new head. Rotate List.

Combinations

  • Fast & Slow Pointers+In-place Reversal

    palindrome list, reorder list

    Fast & Slow Pointers:
    finds the middle
    In-place Reversal:
    reverses the second half

    Palindrome Linked ListReorder List

Follow-ups interviewers ask

  1. 01Reverse in groups of k instead of the whole list? → reverse each full block, recurse/iterate on the rest, stitch boundaries.
  2. 02What if it were a doubly linked list? → also fix prev pointers; a single swap of next/prev per node reverses it.
  3. 03Can you do it recursively, and what's the space cost? → yes, but O(n) stack vs O(1) iterative — discuss the tradeoff.
  4. 04What if the final partial group should also be reversed? → drop the 'k full nodes' guard and reverse whatever remains.

State table

step prev curr saved next structure
init None 1 — 1→2→3
1 1 2 2 1→None, rest 2→3
2 2 3 3 2→1→None
3 3 None — 3→2→1→None ✓

Resources

  • Linked List explore cardLeetCode · course
  • Linked list cheatsheetTech Interview Handbook · article
  • Linked list visualizerVisuAlgo · tool
  • Cycle detection (Floyd/Brent)Wikipedia · article
  • NeetCode RoadmapNeetCode · sheet
Fast & Slow PointersMerge Intervals
TodayReviewPatternsProblems

Template · clean C++, Java and Python to revise from

Start the free 7-day trial