Skip to content
DSA in JavaScript
← All patterns

In-Place Reversal

0/14 solved

What it is

Reverse the arrows of a linked list one node at a time by keeping track of the previous, current and next node. No extra list is needed.

Used for

Reverse a list or a part of it, reverse in groups of k, rotate a list, palindrome checks.
1cur
→
2
→
3
→
4

Step 1 of 6

Goal: reverse 1 → 2 → 3 → 4 so it reads 4 → 3 → 2 → 1, without building a new list. We only flip each arrow, using two helpers: prev (the node behind) and cur (the node we are looking at).

You see

  • Reverse a linked list or part of it
  • Rewire pointers with O(1) extra space
  • Reverse in groups of k

Learn it in

Reversed Linked List TraversalLinked List

Template

function reverseList(head) {  let prev = null;  while (head) {    [head.next, prev, head] = [prev, head, head.next];  }  return prev;}

Practice problems