Reverse Linked List Visualizer & Step-by-Step Algorithm Solution

Reverse a singly linked list iteratively in-place by reversing the next pointer of each node.

Category: linked-list | Difficulty: Easy

Tags: Linked List, Two Pointers, Iterative, In-Place

Reverse Linked List

head
1
2
3
4
5
100%
state
prevundefined
currundefined
nextundefined
Initialization
1/30
Explanation

Initialize reverseList with the head of the singly linked list.

Source Code
1function reverseList(head: ListNode | null): ListNode | null {
2 let prev: ListNode | null = null;
3 let curr = head;
4
5 while (curr !== null) {
6 const next = curr.next;
7 curr.next = prev;
8 prev = curr;
9 curr = next;
10 }
11
12 return prev;
13}