0
0
PythonHow-ToBeginner · 3 min read

How to Reverse a Linked List in Python: Simple Guide

To reverse a linked list in Python, you iterate through the list while changing each node's next pointer to point to the previous node. This process continues until all nodes are reversed, and the last node becomes the new head of the list.
📐

Syntax

The basic syntax to reverse a linked list involves three pointers: prev, current, and next_node. You start with prev = None and current = head. In each step, you save the next node, point current.next to prev, then move prev and current one step forward until current is None.

python
prev = None
current = head
while current:
    next_node = current.next
    current.next = prev
    prev = current
    current = next_node
head = prev
💻

Example

This example shows a simple linked list class and how to reverse it using the method described. It prints the list before and after reversing.

python
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, value):
        if not self.head:
            self.head = Node(value)
            return
        current = self.head
        while current.next:
            current = current.next
        current.next = Node(value)

    def print_list(self):
        current = self.head
        while current:
            print(current.value, end=' -> ' if current.next else '\n')
            current = current.next

    def reverse(self):
        prev = None
        current = self.head
        while current:
            next_node = current.next
            current.next = prev
            prev = current
            current = next_node
        self.head = prev

# Create and print original list
ll = LinkedList()
for i in [1, 2, 3, 4, 5]:
    ll.append(i)
print('Original list:')
ll.print_list()

# Reverse and print reversed list
ll.reverse()
print('Reversed list:')
ll.print_list()
Output
Original list: 1 -> 2 -> 3 -> 4 -> 5 Reversed list: 5 -> 4 -> 3 -> 2 -> 1
⚠️

Common Pitfalls

Common mistakes include forgetting to update the next pointer before moving to the next node, which can cause loss of the rest of the list. Another error is not updating the head of the list after reversal, leaving the list head pointing to the old first node.

python
def wrong_reverse(head):
    current = head
    prev = None
    while current:
        # Missing saving next node causes loss of list
        current.next = prev
        prev = current
        current = current.next  # This is wrong, current.next was changed
    return prev

# Correct way:
def correct_reverse(head):
    prev = None
    current = head
    while current:
        next_node = current.next  # Save next node first
        current.next = prev
        prev = current
        current = next_node
    return prev
📊

Quick Reference

  • Use three pointers: prev, current, and next_node.
  • Always save next_node before changing current.next.
  • Update the head to prev after the loop ends.
  • Test with small lists to ensure correctness.

Key Takeaways

Reverse a linked list by redirecting each node's next pointer to the previous node.
Always save the next node before changing pointers to avoid losing the list.
Update the head pointer to the last processed node after reversal.
Test your reversal function with simple lists to catch mistakes early.