Imagine you have a playlist of songs that repeats forever. Which feature of a circular linked list makes it perfect for this?
Think about what happens when you reach the end of a playlist and want to start over.
A circular linked list connects the last node back to the first, enabling endless traversal without stopping. This is ideal for repeating playlists.
What will be the output after traversing 7 nodes starting from head in this circular linked list?
class Node { constructor(value) { this.value = value; this.next = null; } } const head = new Node(10); const second = new Node(20); const third = new Node(30); head.next = second; second.next = third; third.next = head; // circular link let current = head; const result = []; for (let i = 0; i < 7; i++) { result.push(current.value); current = current.next; } console.log(result.join(' -> ') + ' -> null');
Remember the list loops back to the start after the last node.
The list has 3 nodes linked circularly. Traversing 7 nodes cycles through 10, 20, 30 repeatedly.
Which property of circular linked lists makes them suitable for implementing round robin CPU scheduling?
Think about how the CPU switches between processes repeatedly in a cycle.
Circular linked lists let the scheduler move from one process to the next endlessly, perfect for fair time-sharing.
What is wrong with this code that inserts a new node after the head in a circular linked list?
function insertAfterHead(head, value) {
const newNode = { value: value, next: null };
newNode.next = head.next;
head.next = newNode;
// Missing update for circular link if head is the only node
}
const head = { value: 1, next: null };
head.next = head; // circular link to itself
insertAfterHead(head, 2);
console.log(head.next.value);Check what happens when the list has only one node and you insert after head.
When the list has one node, inserting after head must update the new node's next to head and maintain circularity. The code misses this.
What will be the printed list after deleting the node with value 20 from this circular linked list?
class Node { constructor(value) { this.value = value; this.next = null; } } const head = new Node(10); const second = new Node(20); const third = new Node(30); head.next = second; second.next = third; third.next = head; // circular link function deleteNode(head, value) { if (!head) return null; let current = head; let prev = null; do { if (current.value === value) { if (prev) { prev.next = current.next; } else { // Deleting head node let tail = head; while (tail.next !== head) { tail = tail.next; } head = current.next; tail.next = head; } break; } prev = current; current = current.next; } while (current !== head); return head; } const newHead = deleteNode(head, 20); let curr = newHead; const output = []; do { output.push(curr.value); curr = curr.next; } while (curr !== newHead); console.log(output.join(' -> ') + ' -> null');
After deletion, the list should still be circular and include all nodes except the deleted one.
The node with value 20 is removed. The list remains circular with nodes 10 and 30 linked.
