Reverse a Linked List

Solve this Problem
Easy10–15 min
Topics
Companies
Practice:LeetCode ↗GFG ↗
Given the head of a singly linked list, flip the direction of every link so the list reads back to front, and return the new head — the node that used to be last. Every node's link needs to point at whatever used to come before it instead of what used to come after. Walking the list once while carrying a "reversed so far" pointer, and re-pointing each node's link back at that pointer before sliding both pointers forward, flips every link in a single pass. The recursive version reaches the same result from the opposite direction: it dives all the way to the last node first, then fixes one link per call as the recursion unwinds back to the front.

Test Case 1:

Input:head = [3, 6, 2, 9]
Output:[9, 2, 6, 3]
Explanation:Every link flips direction — the last node becomes the new head.

Test Case 2:

Input:head = [7, 1]
Output:[1, 7]
Explanation:A two-node list simply swaps which node comes first.

Test Case 3:

Input:head = []
Output:[]
Explanation:An empty list has nothing to reverse.

Constraints

  • 0 ≤ number of nodes in head ≤ 200
  • -1000 ≤ node value ≤ 1000
🚀

Try the Dry Run

Don't just read the solution — watch it execute, one step at a time.

Approach & Solutions

Recursive — Reverse the Rest, Then Fix This Node

Good

Recurse all the way to the last node first — that node becomes the new head, and it's returned unchanged back up through every call. As each call returns, it fixes exactly one link: the node just after the current one (which is now the tail of the already- reversed rest) gets its .next pointed back at the current node, and the current node's own .next is cleared so it becomes the new tail. Repeating this on the way back up through every stack frame reverses the whole list, though each frame held on the call stack costs O(n) space for a long list.

TimeO(n)
SpaceO(n)
1// Node definition used in this problem: 2// class Node { 3// int val; 4// Node next; 5// } 6 7class Solution { 8 public Node reverseList(Node head) { 9 if (head == null || head.next == null) return head; 10 Node newHead = reverseList(head.next); 11 head.next.next = head; 12 head.next = null; 13 return newHead; 14 } 15}

Iterative — Three Pointers, Single Pass

Optimal

Walk the list once, carrying two pointers: prev (the reversed portion built so far, starting empty) and curr (the next node to absorb into it). At each node, save curr.next before overwriting it — otherwise the rest of the list would be lost — then point curr.next back at prev, and slide both prev and curr forward by one. Once curr runs off the end, prev is sitting on the last node visited, which is now the new head of the fully reversed list.

TimeO(n)
SpaceO(1)
1// Node definition used in this problem: 2// class Node { 3// int val; 4// Node next; 5// } 6 7class Solution { 8 public Node reverseList(Node head) { 9 Node prev = null; 10 Node curr = head; 11 while (curr != null) { 12 Node next = curr.next; 13 curr.next = prev; 14 prev = curr; 15 curr = next; 16 } 17 return prev; 18 } 19}

Related Problems