Stop 12 · Data Structures & AlgorithmsTopics · 6/11

Linked Lists

Part of the Python Roadmap

In short

Python's list hides all the pointer juggling from you, but interviewers love linked lists because they test whether you can reason about references step by step. Reverse one confidently and many other problems become easy.

A linked list is a chain of nodes where each node points to the next. Python doesn't have one built in, but interviewers often ask you to build one with a small Node class and reverse, merge or detect cycles in it.

The jaadu follows a winding trail of paper chits tied together with red string across a living room, holding one chit up and peering towards the next.

Think of it like this

The treasure hunt chits

At a birthday treasure hunt, each chit tells you where the next chit is hidden: under the sofa, then behind the fridge, then in the shoe rack. You can't jump to chit five; you follow the chain. A linked list is that chain, and each node holds a value plus a pointer to the next.

Watch it happen

Reversing 10 → 20 → 30

10
0
curr
20
1
30
2

1/4Start: prev is None, curr is the head (10). Each arrow will be flipped one at a time.

The big ideas

What you need to know

A node is tiny

A small class holding value and next. The last node's next is None.

Cheap at the front

Adding or removing at the head is O(1). Reaching the k-th item is O(k), because you must walk there.

Three-pointer reverse

Keep prev, curr and next. Save next, flip curr.next, then move all three forward.

Fast and slow pointers

Move one pointer one step and another two steps. If they ever meet, the list has a cycle.

See it in code

Walk through the code

python
1from __future__ import annotations2 3from dataclasses import dataclass4 5 6@dataclass7class Node:8    value: int9    next: Node | None = None10 11 12def reverse(head: Node | None) -> Node | None:13    prev: Node | None = None14    curr = head15    while curr is not None:16        nxt = curr.next17        curr.next = prev18        prev = curr19        curr = nxt20    return prev21 22 23def to_list(head: Node | None) -> list[int]:24    out: list[int] = []25    while head is not None:26        out.append(head.value)27        head = head.next28    return out29 30 31chain = Node(10, Node(20, Node(30)))32print(to_list(chain))           # [10, 20, 30]33print(to_list(reverse(chain)))  # [30, 20, 10]

1/4A dataclass node. The __future__ import lets the class mention Node inside its own definition.

The classic interview question, done in O(n) time and O(1) extra space.

Save yourself the pain

Common mistakes

Oops

Setting curr.next = prev before saving curr.next.

Do this instead

Save it first: nxt = curr.next. Otherwise the rest of the list is unreachable.

Oops

Forgetting to handle an empty list or a single node.

Do this instead

Test with None and one node. The while loop above handles both naturally.

Oops

Using a linked list in everyday Python code.

Do this instead

For real programs, list or deque is almost always better. Linked lists are mainly for interviews and understanding pointers.

Your mission

Linked list drills

~40 min

Quick check

What is the time complexity of reaching the 1,000th node of a linked list?

Go deeper

  1. 1Write a Node class and build a list of five nodes.
  2. 2Reverse a linked list iteratively.
  3. 3Detect a cycle with the slow and fast pointer technique.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge