VLVisual Learner

Circular Linked List

Search: O(n)Insert after a node: O(1)Insert / delete at head: O(n) without a tail pointerNo null at the end
Insert
–
Pseudocode
1newNode ← Node(v)
2if list is empty: newNode.next ← newNode; head ← newNode
3else if index == 0: last ← node whose next is head
4 newNode.next ← head; last.next ← newNode; head ← newNode
5else: curr ← head; move index − 1 steps
6 newNode.next ← curr.next; curr.next ← newNode
Pointer moves
0
Comparisons
0
Nodes
0
node being looked atcurrpointer variablefound / inserted

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.

class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

class CircularLinkedList {
  constructor() {
    this.head = null;   // any node can be the "start"; we keep one
    this.size = 0;
  }

  // the last node is the one whose next is head
  lastNode() {
    let curr = this.head;
    while (curr.next !== this.head) curr = curr.next;
    return curr;
  }
}

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.

insert(data, index = this.size) {
  const newNode = new Node(data);

  if (this.head === null) {           // empty: point to itself
    newNode.next = newNode;
    this.head = newNode;
  } else if (index === 0) {           // new head: fix the last node too
    const last = this.lastNode();
    newNode.next = this.head;
    last.next = newNode;
    this.head = newNode;
  } else {                            // middle or end
    let curr = this.head;
    for (let i = 0; i < index - 1; i++) curr = curr.next;
    newNode.next = curr.next;
    curr.next = newNode;
  }
  this.size++;
}

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).

search(value) {
  if (this.head === null) return -1;
  let curr = this.head;
  let i = 0;
  do {
    if (curr.data === value) return i;
    curr = curr.next;
    i++;
  } while (curr !== this.head);   // back at the start: stop
  return -1;
}

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.

removeAt(index) {
  if (this.head === null || index < 0 || index >= this.size) return false;

  if (this.size === 1) {              // only node
    this.head = null;
  } else if (index === 0) {           // removing head: fix the last node
    const last = this.lastNode();
    this.head = this.head.next;
    last.next = this.head;
  } else {
    let curr = this.head;
    for (let i = 0; i < index - 1; i++) curr = curr.next;
    const target = curr.next;
    curr.next = target.next;          // skip over it
  }
  this.size--;
  return true;
}