Linked Lists in Python
Chapter 41: Linked Lists
4 short lessons, with 3D animations, quizzes and coding tasks.
The lessons
Nodes and links: building and walking a linked list
A linked list is a chain of nodes, where each node keeps a value and a reference to the next node, and the whole list is known by its first node, the head.
Inserting and deleting by changing links
To insert or delete in a linked list, walk to the node just before the spot and change one or two next links; nothing else moves.
Reversing a list, one link at a time
Walk the list once with three markers, and at each node save the rest of the list, turn the node's link round to point backwards, and step on.
Fast and slow pointers: the middle, and finding a cycle
Start slow and fast at the head, move slow 1 node and fast 2 nodes per turn: if fast reaches the end, slow is at the middle; if fast ever lands on the same node as slow, the list has a cycle.