Circular Linked List
1newNode ← Node(v)2if list is empty: newNode.next ← newNode; head ← newNode3else if index == 0: last ← node whose next is head4 newNode.next ← head; last.next ← newNode; head ← newNode5else: curr ← head; move index − 1 steps6 newNode.next ← curr.next; curr.next ← newNode
Circular Linked List Operations
Like a singly linked list, but the last node's next points back to the head, so the nodes form a ring with no null at the end.
Inserting after a node is the same two pointer updates as a singly list. A new head also needs the last node re-pointed, so the ring stays closed.
There is no null to stop at, so the search stops when it arrives back at head after one full lap.
Skip over the node like in a singly list. Removing the head means moving head forward and re-pointing the last node; removing the only node empties the list.
Setup - Node Structure
Each node holds a value and a next pointer, exactly like a singly linked list. The difference is the end: instead of null, the last node points back to the head. That makes the list a loop, which is handy for things that naturally go round and round: players taking turns, a round-robin CPU scheduler, or a playlist on repeat. Use Rotate in the visualizer to see that moving the head is enough to change where the list "starts": no node has to move.
Insert - Add Operation
Inserting after an existing node takes the usual two pointer updates: the new node points to what came next, and the node before it points to the new node. The special cases are the ends of the ring. Into an empty list, the new node simply points to itself. As the new head, the last node must point to it, otherwise the ring would still close at the old head. Finding the last node means walking all the way round, which is why many implementations also keep a tail pointer.
Search - Find Operation
A search walks from the head comparing each value, just like a singly list, but the stopping rule is different. A while (curr != null) loop would spin forever, because there is no null. Instead a do-while loop checks a node, moves on, and stops when it comes back to the head. Worst case it visits every node once: O(n).
Delete - Remove Operation
To delete, find the node before the target and make it skip over the target. Deleting the head also moves the head pointer forward, and the last node has to be re-pointed to the new head. If the list had only one node (which points to itself), deleting it leaves an empty list with head set to null.