Doubly and Circular Linked Lists

Lesson 10 of 20

Rewire prev and next to splice 27 into a doubly chain, then chase curr around a ring that never hits None.

The big picture

Two-way chains

Give every node a second pointer and the chain grows a backward gear; point the tail at the head and it becomes a ring with no exit.

Two directions

A DNode stores prev AND next — walk forward from head or backward from the tail.

Deletion for free

Hold node n? n.prev.next = n.next and n.next.prev = n.prev — O(1), no hunt for the predecessor.

Circular rings

tail.next = head closes the loop — there is no None, so traversal needs its own stop test.

Round-robin

Serve curr, advance, repeat: CPU schedulers and board-game turns walk a ring forever.

The sentinel trick

A dummy head node that never gets deleted erases every empty-list special case.