Nodes and links
A linked list keeps each item in its own small object called a node. A node holds two things: the value, here 3, and next, a reference (an arrow) to the node after it. Nodes can sit anywhere in memory.
head is a variable that refers to the first node. Each next points to the following node: 3, then 7, then 9. The last next is None, which marks the end. Python built this list with Node(3, Node(7, Node(9))).
Working on a list
There is no index to jump to. To find 7, node starts at head and follows next, one link at a time, until it holds 7. Reaching position k takes k hops: O(n) in the worst case, where a Python list index takes O(1).
Node(1, head) makes a new node whose next is the old first node, 3. Then head moves to the new node. That is two link writes however long the list is: O(1). A Python list's insert(0, x) moves every item over: O(n).
To put 8 after 7, the new node first points at 7's old next, 9. Then 7's next moves to the new node. Nothing shifts in memory; two links change. Do it in this order, or 9 is lost.
To remove 3, link around it: old holds node 3, then 1's next jumps over it to old.next, 7. No link in the list reaches 3 now, so once old is dropped Python can free it. No shifting, just one write.
Two more shapes
A doubly linked list gives each node a second link, prev, to the node before it. You can now walk backwards, and unlink a node you hold in O(1): node.prev.next = node.next, then node.next.prev = node.prev. The price: one more reference per node.
Back to single links. In a circular list the last node's next points to the first instead of None. A walk never runs off the end, so a loop must stop when it is back at the start. It suits anything that takes turns.
What happens
;; step 1/8 · node
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
Node(3) makes one node:
value = 3
next = None until we link it
a reference is how Python names an object: an arrow, not a copy
Costs follow MIT 6.006 (Lecture 2): a singly linked list inserts and deletes at the front in Θ(1) but needs O(n) to get or set the item at position i. Inserting or removing after a node you already hold is O(1); finding that node is the O(n) part. Python's built-in list is an array of references, not a linked list: insert(0, x) and pop(0) move the other items, O(n). For fast adds and removes at both ends the standard library has collections.deque, which gives about O(1) appends and pops on either side.