How to Reverse a Linked List

Learn How to Reverse a Linked List with practical examples, explanations, best practices, and real-world coding techniques.

What Is Linked List Reversal?

Linked list reversal is the process of changing the direction of the links between nodes so that the last node becomes the first node and the original first node becomes the last node. The nodes themselves do not need to be recreated or moved in memory. Instead, their next references are changed.

For a singly linked list, reversal is a fundamental operation because it teaches how to manipulate node references safely. It is also a common interview problem and appears as part of larger linked list algorithms.

Before and After Reversal

Consider the following singly linked list:

10 → 20 → 30 → 40 → null

After reversal, the direction of every link changes:

40 → 30 → 20 → 10 → null

The important point is that the algorithm changes the references between existing nodes. It does not simply reverse an array of values.

The Three References Used in Reversal

The standard iterative reversal algorithm uses three references: previous, current, and next.

  • previous stores the node that should come before the current node.
  • current represents the node currently being processed.
  • next temporarily saves the remaining part of the list.

The next reference is especially important. If the current node's next reference is changed before saving the original next node, the remaining part of the list can become inaccessible.

Iterative Linked List Reversal

The iterative approach reverses one connection at a time. Initially, previous is null and current starts at the head.

let previous = null;
let current = head;

For every node, the algorithm follows four important steps:

  1. Save the next node.
  2. Reverse the current node's reference.
  3. Move previous forward.
  4. Move current forward.
while (current !== null) {
  const next = current.next;

  current.next = previous;

  previous = current;
  current = next;
}

head = previous;

Step-by-Step Reversal

Suppose the list is:

10 → 20 → 30 → null

Initially:

previous = null
current = 10

Save the next node and reverse the first connection:

next = 20
10.next = null

previous = 10
current = 20

The partially reversed portion is now:

10 → null

20 → 30 → null

The same process is repeated for 20:

next = 30
20.next = 10

previous = 20
current = 30

The reversed portion becomes:

20 → 10 → null

30 → null

Finally, the last node is processed:

next = null
30.next = 20

previous = 30
current = null

Since current is now null, the reversal is complete. The head becomes previous:

30 → 20 → 10 → null

Complete Iterative Implementation

The following implementation reverses a singly linked list in place.

class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

function reverseLinkedList(head) {
  let previous = null;
  let current = head;

  while (current !== null) {
    const next = current.next;

    current.next = previous;

    previous = current;
    current = next;
  }

  return previous;
}

let head = new Node(10);

head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);

head = reverseLinkedList(head);

let current = head;

while (current !== null) {
  console.log(current.data);
  current = current.next;
}

The output is:

40
30
20
10

Reversing a Linked List in a Class

If a linked list maintains both head and tail, the reversal should update both references. The original head becomes the new tail, while the original tail becomes the new head.

reverse() {
  let previous = null;
  let current = this.head;

  this.tail = this.head;

  while (current !== null) {
    const next = current.next;

    current.next = previous;

    previous = current;
    current = next;
  }

  this.head = previous;
}

Consider:

head → 10 → 20 → 30 → 40 → null
                              ↑
                             tail

After reversal:

head → 40 → 30 → 20 → 10 → null
                              ↑
                             tail

Recursive Linked List Reversal

A linked list can also be reversed recursively. The recursive approach first reaches the end of the list and then reverses the references while returning from the recursive calls.

function reverseRecursive(head) {
  if (head === null || head.next === null) {
    return head;
  }

  const newHead = reverseRecursive(head.next);

  head.next.next = head;
  head.next = null;

  return newHead;
}

The base case handles an empty list or a list containing only one node. For larger lists, the recursive call reverses the remaining portion first. The current node is then attached after the node that originally followed it.

Understanding the Recursive Connection

Suppose the list is:

10 → 20 → 30 → null

The recursive calls continue until 30 is reached. When the calls return, the relationship between 20 and 30 is reversed:

30 → 20

Then the relationship between 10 and 20 is reversed:

30 → 20 → 10 → null

The old head becomes the new tail, so its next reference must eventually be set to null.

Iterative vs Recursive Reversal

Aspect Iterative Recursive
Time O(n) O(n)
Extra space O(1) O(n) call stack
Main technique Reference manipulation in a loop Recursive calls and backtracking
Typical use Memory-efficient reversal Understanding recursive linked list problems

Reversing an Empty or Single-Node List

Reversal should handle edge cases without requiring special complex logic.

For an empty list:

null → null

For a single-node list:

10 → null

becomes

10 → null

The iterative algorithm naturally handles both cases because the loop either does not execute or processes one node.

Why the Next Reference Must Be Saved

One of the most important details in linked list reversal is saving the original next reference before changing it.

Consider:

10 → 20 → 30 → null

If you immediately execute:

current.next = previous;

the original connection from 10 to 20 is gone. Without another reference to 20, the remaining list cannot be traversed.

That is why the correct order is:

const next = current.next;
current.next = previous;

Reversal Complexity

Approach Time Extra Space
Iterative O(n) O(1)
Recursive O(n) O(n)

Both approaches visit each node once. The difference is the additional call-stack space required by the recursive implementation.

Common Linked List Reversal Mistakes

  • Changing current.next before saving the original next node.
  • Forgetting to update the head after the reversal.
  • Forgetting to set the old head's next reference to null.
  • Using the wrong order when updating previous and current.
  • Forgetting to update the tail when the linked list stores a tail reference.
  • Not handling an empty linked list or a single-node list.
  • Returning the old head instead of the new head.

A Reusable Reversal Pattern

The iterative reversal algorithm can be remembered as a four-step reference pattern:

1. Save next
2. Reverse current.next
3. Move previous
4. Move current

In code:

const next = current.next;
current.next = previous;
previous = current;
current = next;

This pattern is worth understanding rather than memorizing because the same reference manipulation appears in many linked list problems.

Where Linked List Reversal Is Used

Reversal is useful beyond the basic standalone operation. It can be combined with other algorithms to solve more complex problems.

  • Checking whether a linked list is a palindrome.
  • Reversing nodes in groups of a fixed size.
  • Reversing a linked list between two positions.
  • Comparing a list with its reversed order.
  • Processing linked-list data from the opposite direction.
  • Solving common linked list interview problems.

Linked List Reversal in Interviews

Reversing a linked list is one of the most common linked list interview problems. It is often used to evaluate whether a developer understands references, traversal, edge cases, and in-place algorithms.

A strong approach is to first explain the three references, show how one link changes at a time, and then implement the iterative solution. Once the basic reversal is understood, variations such as partial reversal and group reversal become easier to approach.

Key Takeaways

  • Linked list reversal changes the direction of node references.
  • The standard iterative approach uses previous, current, and next references.
  • The original next node must be saved before reversing the current reference.
  • Iterative reversal takes O(n) time and O(1) extra space.
  • Recursive reversal takes O(n) time and O(n) call-stack space.
  • The original head becomes the new tail after reversal.
  • The new head is the last node of the original linked list.
  • Reversal is a foundation for several advanced linked list problems.

Code Examples

function reverseLinkedList(head) {
  let previous = null;
  let current = head;

  while (current !== null) {
    const next = current.next;

    current.next = previous;

    previous = current;
    current = next;
  }

  return previous;
}
    
function reverseRecursive(head) {
  if (head === null || head.next === null) {
    return head;
  }

  const newHead = reverseRecursive(head.next);

  head.next.next = head;
  head.next = null;

  return newHead;
}
    
class LinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  reverse() {
    let previous = null;
    let current = this.head;

    this.tail = this.head;

    while (current !== null) {
      const next = current.next;

      current.next = previous;

      previous = current;
      current = next;
    }

    this.head = previous;
  }
}
    

Pro Tip

For iterative linked list reversal, remember the order: save next, reverse current.next, move previous, then move current. Changing the order can disconnect the remaining list.

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.