Consider a singly linked list initially empty. We push nodes with values 10, 20, then 30 in that order. What is the printed state of the linked list?
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; push(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; } else { let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } } printList(): string { let result = ''; let current = this.head; while (current) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const list = new LinkedList(); list.push(10); list.push(20); list.push(30); console.log(list.printList());
Remember, pushing adds nodes at the end of the list.
Each push adds a new node at the end. So the order is 10, then 20, then 30.
We push nodes with values 5, 5, 10 into an empty linked list. What is the printed state?
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; push(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; } else { let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } } printList(): string { let result = ''; let current = this.head; while (current) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const list = new LinkedList(); list.push(5); list.push(5); list.push(10); console.log(list.printList());
Duplicates are allowed and pushed at the end.
Push adds nodes at the end, so duplicates appear in order pushed.
Examine the push method below. Why does it cause an infinite loop when pushing nodes?
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; push(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; } else { let current = this.head; while (current.next !== null) { current = current.next; } current.next = current; // bug here } } }
Check what current.next points to after assignment.
Assigning current.next = current creates a cycle where a node points to itself, causing infinite traversal.
Consider a singly linked list with no tail pointer. What is the time complexity of the push operation that adds a node at the end?
Think about how many nodes you must traverse to reach the end.
Without a tail pointer, you must traverse all nodes to find the end, which takes linear time O(n).
Given the linked list after pushing 1, 2, 3, what is the printed state after reversing the list?
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; push(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; } else { let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } } reverse() { let prev: Node | null = null; let current = this.head; while (current) { const nextNode = current.next; current.next = prev; prev = current; current = nextNode; } this.head = prev; } printList(): string { let result = ''; let current = this.head; while (current) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const list = new LinkedList(); list.push(1); list.push(2); list.push(3); list.reverse(); console.log(list.printList());
Reversing flips the order of nodes.
The reverse method changes the next pointers so the list order is reversed.
