How linked-list questions give you the chain

Linked lists Junior Python 3.13+ Reviewed By Best Latest Apps

Predict the output

What does this print?

Python 3.13
from dataclasses import dataclass


@dataclass(eq=False)
class Link:
    val: int
    next: "Link | None" = None


head = Link(1, Link(2, Link(3)))
head.next = head.next.next if head.next else None
vals = []
cur: Link | None = head
while cur:
    vals.append(cur.val)
    cur = cur.next
print(vals)

Options

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A [1, 3]

Right: the arrow from 1 now skips over 2.

Removing a link from a chain is one arrow change; nothing is shifted, which is the main contrast with an array.

Why not the others?
  • B [1, 2, 3]. The second link is bypassed by the reassignment.
  • C [2, 3]. The head itself is still the link holding 1.

30-second answer

Each link is a small Link object with a value and a next reference; the last link's next is None. Pages take the head link. To test, build a chain from a Python list with a short helper and read it back with another, so test cases stay readable.

Practise saying it · 30 s. No recording, just a timer

Explain how chains are represented.

Say your answer out loud, then check which of these points you covered:

  • Link with val and next
  • Head is the entry point
  • Build from a list to test

Deep dive

The same two helpers sit behind every linked-list page, so the tests use plain lists.

eq=False keeps identity comparison, which loop checks need.

On a whiteboard, draw boxes and arrows first; it is faster than reasoning in code.

The Link class and the helpers Ran on Python 3.13.5 · mypy --strict
Python 3.13
from dataclasses import dataclass


@dataclass(eq=False)
class Link:
    val: int
    next: "Link | None" = None  # None at the tail


def build(vals: list[int]) -> Link | None:
    head: Link | None = None
    for v in reversed(vals):
        head = Link(v, head)
    return head


def to_vals(head: Link | None) -> list[int]:
    out: list[int] = []
    while head is not None:
        out.append(head.val)
        head = head.next
    return out


print(to_vals(build([4, 7, 1])))

Common mistakes

  • Printing a looping chain, which never ends.
  • Comparing links with == when you mean the same object (is).

Senior take & follow-ups

Offer to write the build helper first; it makes your own tests quick.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why not use a Python list in the answer?

    Show hint

    The question is about pointer moves with O(1) extra memory; a list copy changes the cost.

  2. How would you print a chain that may loop?

    Show hint

    Stop after a step limit or track visited links in a set.

What each operation costs: Linked List · Singly Linked List · Doubly Linked List

From the author

DSA Master - Data & Algorithms

Free on iPhone and iPad: watch 50+ data structures and algorithms step through animations, run the code in the playground, and try the daily challenge.

Suggest a fix if something here is wrong or out of date.