Fetching latest headlines…
Reversing a Linked list
NORTH AMERICA
πŸ‡ΊπŸ‡Έ United Statesβ€’March 22, 2026

Reversing a Linked list

0 views0 likes0 comments
Originally published byDev.to

Reversing a Linked List

  1. Reversing a linked list is one of the most fundamental problems in data structures. It helps build a strong understanding of pointer manipulation and is frequently asked in interviews.

  2. In this article, we will break down how to reverse a singly linked list using an iterative approach.

Understanding the Problem

Given a linked list like this:

1 -> 2 -> 3 -> 4 -> 5

We want to reverse it so that it becomes:

5 -> 4 -> 3 -> 2 -> 1
Structure of a Node

class Node:
    def __init__(self, newData):
        self.data = newData
        self.next = None

def reverseList(head):
    if head is None or head.next is None:
        return head

    # reverse the rest of linked list and put the
    # first element at the end
    rest = reverseList(head.next)

    # make the current head as last node of
    # remaining linked list
    head.next.next = head

    # update next of current head to NULL
    head.next = None

    return rest


def printList(node):
    while node is not None:
        print(f"{node.data}", end="")
        if node.next is not None:
            print(" -> ", end="")
        node = node.next
    print()


if __name__ == "__main__":

    # Create a hard-coded linked list:
    # 1 -> 2 -> 3 -> 4 -> 5
    head = Node(1)
    head.next = Node(2)
    head.next.next = Node(3)
    head.next.next.next = Node(4)
    head.next.next.next.next = Node(5)

    head = reverseList(head)
    printList(head)

***Step-by-Step Explanation*

  1. Initialization** curr = head prev = None curr starts from the head of the list prev is initially None because the new last node will point to nothing
  2. Traverse the List while curr is not None:

We iterate through each node until we reach the end.

  1. Store the Next Node nextNode = curr.next

We store the next node so we do not lose access to the remaining list.

  1. Reverse the Link curr.next = prev

This is the key step.

We reverse the pointer so that the current node now points to the previous node instead of the next one.

  1. Move Pointers Forward prev = curr curr = nextNode Move prev to the current node Move curr to the next node
  2. Return New Head return prev

At the end of the loop, prev will be pointing to the new head of the reversed list.

Printing the List
def printList(node):
while node is not None:
print(f"{node.data}", end="")
if node.next is not None:
print(" -> ", end="")
node = node.next
print()

This function helps visualize the linked list.

Example Execution
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)

head = reverseList(head)
printList(head)

Output:

5 -> 4 -> 3 -> 2 -> 1
Why This Approach is Used

This iterative method is preferred because:

It uses constant extra space
It is efficient and runs in linear time
It does not require recursion, so no stack overhead
Key Insight

The main idea is to reverse the direction of the next pointer for each node while carefully preserving access to the rest of the list.

Comments (0)

Sign in to join the discussion

Be the first to comment!