Sitemap
Alan Martins

Alan Martins

Senior Software Developer at Nubank

Reverse Linked List

3 min readJun 23, 2025

--

How to Reverse a Linked List?

Reversing a linked list is a fundamental problem in coding interviews. It’s an essential skill because it helps you understand how to work with pointers, traverse a list, and is often used as part of more complex problems.

Understanding the Problem

Let’s take a simple linked list: 1 -> 2 -> 3 -> 4. After reversing it, the result should be 4 -> 3 -> 2 -> 1.

To make it even simpler, imagine reversing just 1 -> 2.

Get Alan Martins’s stories in your inbox

Join Medium for free to get updates from this writer.

Currently, Node 1 points to Node 2, and Node 2points to null.
After reversing, Node 1 should point to null, and Node 2 should point to Node 1.

To do this, we need to traverse the list and update the pointers for each node. At the end, we return the last node, which becomes the new head of the reversed list.

How the Algorithm Works

The problem can be broken down into simple steps:

  • Set the previous and current pointers.
    previous starts as null (since nothing comes before the head).
    current starts as the first node (Node 1).
  • Start at the first node (Node 1).
    Node 1 is the current.
    previous is null because nothing is pointing to Node 1.
  • Reverse the pointer for the current node.
    MakeNode 1 point to previous (which is null).
    — Move the pointers forward:
    — previous becomes Node 1.
    — current becomes Node 2.
  • Repeat the process for the following nodes.
  • Make Node 2 point to Node 1
  • Make previous to Node 2 and current to Node 3
  • Continue until current becomes null.
    This means we have reached the end of the list.
  • The previous is now the new head of the reversed list.
    - Return previous

Step-by-Step Example

Step 1: Initialize pointers

  • Create two pointers:
    previous = None (no node before the head)
    current = head (start from the head of the list)
previous = None
current = head

Step 2: Traverse and reverse pointers

  • While current is not None:
    Save the next node in a temporary variable.
    Reverse the pointer (current.next = previous).
    Move previous to current.
    Move current to the next node (saved in the temp variable).
while current:
next_node = current.next
current.next = previous
previous = current
current = next_node

Step 3: Return the new head

  • After the loop ends, previous will be the new head of the reversed list.
return previous

This solution has O(n) time complexity because it traverses the list once, and O(1) space complexity because it uses only a few pointers.

Full solution

class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next

def reverseList(head):
previous = None
current = head
while current:
next_node = current.next
current.next = previous
previous = current
current = next_node
return previous

Can You Solve It with Recursion?

This problem can also be solved using recursion instead of a loop. :)

References

--

--