← Back to Codes

2026-06-09 16:36:42

Reverse Linked List in Python and Java | LeetCode #206

Learn how to solve Reverse Linked List in Python and Java with beginner-friendly explanation, step-by-step dry run, custom examples, and time complexity.

Reverse Linked List in Python and Java

In this tutorial, we will learn LeetCode #206: Reverse Linked List in very simple language. We will understand the idea step by step, see custom examples, and write complete code in Python and Java.

What Is the Reverse Linked List Problem?

This problem asks us to solve a common coding interview task using the given input. The goal is to return the correct result without using a slow brute force method.

For this problem, we will use pointer reversal because it gives a clean and optimized solution.

Example input:
head = [4, 7, 9, 2]

Expected result:
Output: [2, 9, 7, 4]

Explanation:
Pointers are reversed.

Beginner-Friendly Idea

The main idea is to avoid trying every possible answer blindly. Instead, we keep useful information while reading the input and use that information to make the next decision.

At each step, ask: “What do I already know, and how does the current value change my answer?”

Using our example:
head = [4, 7, 9, 2]

Approach:
pointer reversal

We update variables step by step until we reach:
Output: [2, 9, 7, 4]

Why Do We Use This Approach?

A direct brute force solution is usually easier to think about, but it can become slow when the input is large. The optimized approach keeps only the important state and avoids repeated work.

That is why pointer reversal is useful for this problem.

Step-by-Step Explanation

Let us dry run the algorithm using a custom example.

Step 1
Use this linked list.

4 -> 7 -> 9 -> 2

Start with:
previous = null
current = 4

Step 2
Reverse the first node.

next_node = 7
current.next = previous
4 -> null

previous = 4
current = 7

Step 3
Reverse the next node.

next_node = 9
7.next = 4
7 -> 4 -> null

previous = 7
current = 9

Step 4
After all nodes are reversed, the list becomes:

2 -> 9 -> 7 -> 4

Final answer = [2, 9, 7, 4]

Important Code Logic

The most important part is updating the algorithm state after reading each useful value. This is where the answer becomes better step by step.

Think like this:

old_state = what we knew before
current_value = value we are checking now
new_state = updated result after using current_value

For our example, the final state gives:
Output: [2, 9, 7, 4]

Example 1

Input:
head = [4, 7, 9, 2]

Output:
Output: [2, 9, 7, 4]

Explanation:
Pointers are reversed.

Example 2

Input:
head = [10, 20, 30]

Output:
[30, 20, 10]

Explanation:
The linked list is reversed by changing pointers.

Python Code

Here is the complete Python solution for LeetCode #206.

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

class LinkedListReverser:
    def reverse_list(self, head):
        previous = None
        current = head

        while current:
            next_node = current.next
            current.next = previous
            previous = current
            current = next_node

        return previous


def build_list(values):
    dummy = ListNode(0)
    current = dummy
    for value in values:
        current.next = ListNode(value)
        current = current.next
    return dummy.next


def to_array(head):
    result = []
    while head:
        result.append(head.val)
        head = head.next
    return result


reverser = LinkedListReverser()
print(to_array(reverser.reverse_list(build_list([4, 7, 9, 2]))))  # Output: [2, 9, 7, 4]

Java Code

Here is the complete Java solution for LeetCode #206.

import java.util.Arrays;
import java.util.ArrayList;

class ListNode {
    int val;
    ListNode next;
    ListNode(int val) { this.val = val; }
}

class LinkedListReverser {
    public ListNode reverseList(ListNode head) {
        ListNode previous = null;
        ListNode current = head;

        while (current != null) {
            ListNode nextNode = current.next;
            current.next = previous;
            previous = current;
            current = nextNode;
        }

        return previous;
    }

    private static ListNode buildList(int[] values) {
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        for (int value : values) {
            current.next = new ListNode(value);
            current = current.next;
        }
        return dummy.next;
    }

    private static String toText(ListNode head) {
        ArrayList<Integer> values = new ArrayList<>();
        while (head != null) {
            values.add(head.val);
            head = head.next;
        }
        return values.toString();
    }

    public static void main(String[] args) {
        LinkedListReverser reverser = new LinkedListReverser();
        ListNode head = buildList(new int[] {4, 7, 9, 2});
        System.out.println(toText(reverser.reverseList(head))); // Output: [2, 9, 7, 4]
    }
}

Time and Space Complexity

Time Complexity: O(n)

The time complexity depends on how many values the algorithm needs to process and whether it uses sorting, binary search, heap, or traversal.

Space Complexity: O(n)

The extra space is used for the variables or data structures needed by the optimized approach.

Final Summary

LeetCode #206: Reverse Linked List becomes easier when we break it into small steps. First understand what the problem asks, then track the important state, dry run with an example, and finally write the code.