Reverse Linked List – LeetCode75 Python Solution Explained

Reversing a linked list is one of the most fundamental problems in data structures and is frequently asked in coding interviews. The Reverse Linked List problem (LeetCode75) tests your understanding of pointer manipulation, iterative traversal and in-place reordering of linked lists.

Reverse Linked List – LeetCode75 Python Solution Explained

In this blog, we’ll break down the problem, explain the logic, provide a complete Python solution, walk through the solution step by step, analyze complexity and discuss real-world applications.

Problem Statement

Given the head of a singly linked list, reverse the list and return its new head.

  • You can reverse the list iteratively or recursively.
  • The goal is to reorder the nodes in-place without creating extra lists.

Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]

Input: head = [1, 2]
Output: [2, 1]

Understanding the Problem

Reversing a linked list means that for every node, we change its next pointer to point to the previous node instead of the next one.

Key ideas:

  1. Keep track of the current node being processed.
  2. Temporarily store the next node so we don’t lose access to the remainder of the list.
  3. Point the current node’s next to the new head (or previous node).
  4. Move the head forward and repeat until the list is fully reversed.

This problem is essential for mastering pointer manipulation and in-place linked list algorithms.

Examples

Example 1

Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]

Example 2

Input: [1, 2]
Output: [2, 1]

Example 3

Input: [1]
Output: [1]

Complete Python Solution

from typing import Optional

# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode() 
        curr = head
        while curr:
            next = curr.next
            curr.next = dummy.next
            dummy.next = curr
            curr = next
        return dummy.next

Step-by-Step Explanation

Step 1: Initialize a Dummy Node

We use a dummy node to build the reversed list. This simplifies pointer manipulation.

dummy = ListNode()
curr = head

Step 2: Traverse the Original List

While there are nodes remaining, we take the current node, store its next node temporarily and reverse its pointer.

while curr:
    next = curr.next
    curr.next = dummy.next
    dummy.next = curr
    curr = next
  • next = curr.next stores the next node.
  • curr.next = dummy.next points the current node to the new list head.
  • dummy.next = curr moves the dummy head forward.
  • curr = next moves to the next node in the original list.

Step 3: Return the Reversed List

The dummy node now points to the head of the reversed list.

return dummy.next

Time and Space Complexity

  • Time Complexity: O(n) → Each node is visited once.
  • Space Complexity: O(1) → No extra memory is used besides pointers.

Edge Cases

  • Empty list → Return None.
  • Single-node list → Return the same node.
  • Two-node list → Properly swap the two nodes.

Real-World Applications

Reversing a linked list is more than a coding exercise. It has practical applications:

  • Undo/Redo functionality in software applications.
  • Stack simulation using linked lists.
  • Reversing sequences in data processing pipelines.
  • Algorithmic building block for more complex problems like reversing sublists or solving palindrome linked list problems.

Conclusion

The Reverse Linked List problem (LeetCode75) is a foundational exercise in pointer manipulation and in-place linked list operations. By carefully adjusting node pointers and using a dummy head, we can reverse a singly linked list efficiently in O(n) time and O(1) space.

Mastering this problem improves your linked list skills, prepares you for technical interviews and strengthens your ability to implement efficient, in-place algorithms that are critical in real-world software development.

Related Reads

References

2 thoughts on “Reverse Linked List – LeetCode75 Python Solution Explained”

Leave a Comment