08 / 16

How do you reverse a Linked List? (Iterative and Recursive)

Difficulty: 3/10

Reversing a Linked List

A linked list can be reversed iteratively by maintaining previous, current, and next references. At each step, the current node's next pointer is redirected toward the previous node. A recursive solution reverses the remaining list first and then reconnects the current node.

javascript
  1. 1

    Iterative time complexity: O(n).

  2. 2

    Iterative auxiliary space: O(1).

  3. 3

    Recursive time complexity: O(n).

  4. 4

    Recursive auxiliary space: O(n) because of the call stack.

  5. 5

    The iterative solution is generally safer for very long lists because it avoids stack overflow.

Follow-up Questions

  • How do you reverse a sublist between two positions?
  • How would you reverse a doubly linked list?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.