Singly linked list — node with value + next
corebeginnerInstead of one contiguous block like a list, a singly linked list is a chain of separate nodes — each one holds its own value plus a pointer to the NEXT node, ending in None.
Think of it as
There is no single memory block to index into. The list is really just node 1 pointing at node 2 pointing at node 3, and so on until a node points at None. To reach node 5, you must walk through nodes 1 through 4 first — there is no shortcut in.
What we're doing: Build a small linked list by hand and traverse it, collecting every value into a plain Python list.
- 7
- Node(1, Node(2, Node(3))) builds the chain inside out — the innermost Node(3) is built first, then wrapped by Node(2), then Node(1).
- 8
- node starts at head and is reassigned to node.next every iteration — the loop stops the instant node becomes None.
[1, 2, 3]Why this works: Node(1, Node(2, Node(3))) wires up head.next to the node holding 2, and that node's next to the node holding 3, whose own next defaults to None — walking .value then .next three times collects every value before the loop hits None and stops.
Collecting the node itself instead of node.value
Wrong
Better
What you see: to_list_wrong(head) returns a list of Node objects, not their values — result[0] is literally the same object as head (True), and reading it needs .value to see anything meaningful.
Why: The loop variable node walks the chain correctly, but appending node itself instead of node.value collects a list of Node objects — real data, technically, but not the values the function promised to collect.
Remember: A linked list is a chain of nodes, not a block of memory — head is the only way in, and every node is only reachable by following .next from something that came before it.