Challenge - 5 Problems
Singly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this linked list after insertions?
Consider a singly linked list where we insert nodes with values 10, 20, and 30 at the end. What will be the printed state of the list?
DSA Typescript
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class LinkedList { head: Node | null = null; insert(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } print() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.insert(10); list.insert(20); list.insert(30); list.print();
Attempts:
2 left
💡 Hint
Remember, inserting at the end means the first inserted node stays at the head.
✗ Incorrect
Nodes are inserted at the end, so the order is 10, then 20, then 30, followed by null.
❓ Predict Output
intermediateWhat is the output after deleting the head node?
Given a singly linked list with nodes 5 -> 15 -> 25 -> null, if we delete the head node, what will be the new printed state?
DSA Typescript
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class LinkedList { head: Node | null = null; insert(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteHead() { if (this.head) { this.head = this.head.next; } } print() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.insert(5); list.insert(15); list.insert(25); list.deleteHead(); list.print();
Attempts:
2 left
💡 Hint
Deleting the head means the second node becomes the new head.
✗ Incorrect
After deleting the head (5), the list starts from 15 and continues to 25.
🔧 Debug
advancedWhy does this linked list insertion code cause an infinite loop?
Examine the following code for inserting a node at the end of a singly linked list. Why does it cause an infinite loop?
DSA Typescript
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class LinkedList { head: Node | null = null; insert(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next !== null) { current = current.next; } current.next = current; // Problem here } }
Attempts:
2 left
💡 Hint
Check what 'current.next = current;' does to the list structure.
✗ Incorrect
Assigning 'current.next = current;' makes the node point to itself, creating a loop that never ends.
🧠 Conceptual
advancedWhat is the time complexity of inserting a node at the end of a singly linked list without a tail pointer?
If a singly linked list does not maintain a tail pointer, what is the time complexity of inserting a new node at the end?
Attempts:
2 left
💡 Hint
Think about how you find the last node without a tail pointer.
✗ Incorrect
Without a tail pointer, you must start at the head and move through all nodes to reach the end, which takes linear time.
🚀 Application
expertAfter these operations, what is the final linked list?
Start with an empty singly linked list. Perform these operations in order:
1. Insert 1 at the end
2. Insert 2 at the end
3. Insert 3 at the end
4. Delete the head node
5. Insert 4 at the end
6. Delete the head node
What is the printed state of the list now?
DSA Typescript
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class LinkedList { head: Node | null = null; insert(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteHead() { if (this.head) { this.head = this.head.next; } } print() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.insert(1); list.insert(2); list.insert(3); list.deleteHead(); list.insert(4); list.deleteHead(); list.print();
Attempts:
2 left
💡 Hint
Track the head node carefully after each deletion.
✗ Incorrect
After first deletion, head moves from 1 to 2; after second deletion, head moves from 2 to 3; list ends with nodes 3 and 4.
