Which statement correctly describes the main difference between a circular linked list and a linear linked list?
Think about how the last node connects in each list type.
A circular linked list connects the last node back to the first node, creating a loop. A linear linked list ends with a node pointing to null.
Given the following TypeScript code for a circular linked list, what will be the output?
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } const node1 = new Node(1); const node2 = new Node(2); const node3 = new Node(3); node1.next = node2; node2.next = node3; node3.next = node1; // circular link let current: Node | null = node1; let count = 0; const output: number[] = []; while (current && count < 5) { // limit to 5 to avoid infinite loop output.push(current.value); current = current.next; count++; } console.log(output.join(' -> ') + ' -> ...');
Notice the loop and the count limit in the while loop.
The circular linked list loops back to the first node after the last. The loop runs 5 times, so the output repeats nodes 1, 2, 3, then 1, 2.
What error will occur when running this TypeScript code that tries to traverse a linear linked list?
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } const node1 = new Node(1); const node2 = new Node(2); const node3 = new Node(3); node1.next = node2; node2.next = node3; node3.next = null; let current: Node = node1; while (current.next !== null) { console.log(current.value); current = current.next; } console.log(current.value);
Check the loop condition and what is printed inside and after the loop.
The loop runs while current.next is not null, printing nodes 1 and 2 inside the loop, then prints node 3 after the loop ends. No error occurs.
What is the output after inserting a new node with value 4 after node 2 in this circular linked list?
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } const node1 = new Node(1); const node2 = new Node(2); const node3 = new Node(3); node1.next = node2; node2.next = node3; node3.next = node1; // circular const newNode = new Node(4); newNode.next = node2.next; node2.next = newNode; let current: Node | null = node1; let count = 0; const output: number[] = []; while (current && count < 7) { output.push(current.value); current = current.next; count++; } console.log(output.join(' -> ') + ' -> ...');
Focus on where the new node is inserted and how the links change.
The new node 4 is inserted after node 2, so the order becomes 1, 2, 4, 3 repeating in the circular list.
Which statement best explains the memory usage difference between circular and linear linked lists?
Consider the size of each node and what pointers they hold.
Each node in both lists stores one pointer. The difference is in pointer value, not memory size. So memory usage per node is the same.
