Challenge - 5 Problems
Linked List Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the length of the linked list after these insertions?
Consider a singly linked list where we insert nodes with values 10, 20, and 30 in that order. What will be the length of the list after these insertions?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; insert(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; } } getLength(): number { let count = 0; let current = this.head; while (current) { count++; current = current.next; } return count; } } const list = new LinkedList(); list.insert(10); list.insert(20); list.insert(30); console.log(list.getLength());
Attempts:
2 left
💡 Hint
Count each node starting from the head until you reach the end.
✗ Incorrect
Each insertion adds one node at the end. After inserting 10, 20, and 30, the list has 3 nodes. The getLength method counts all nodes by traversing from head to the end.
❓ Predict Output
intermediateWhat is the length of an empty linked list?
If you create a new linked list but do not insert any nodes, what will be the length returned by getLength()?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; getLength(): number { let count = 0; let current = this.head; while (current) { count++; current = current.next; } return count; } } const list = new LinkedList(); console.log(list.getLength());
Attempts:
2 left
💡 Hint
No nodes means length is zero.
✗ Incorrect
Since no nodes are inserted, the head is null and the traversal loop does not run. The count remains zero.
🔧 Debug
advancedWhy does this getLength method return 0 even after insertions?
The following code inserts nodes but getLength always returns 0. What is the bug?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; insert(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; } } getLength(): number { let count = 0; let current = null; while (current) { count++; current = current.next; } return count; } } const list = new LinkedList(); list.insert(5); list.insert(15); console.log(list.getLength());
Attempts:
2 left
💡 Hint
Check where the traversal starts in getLength.
✗ Incorrect
The getLength method sets current = null instead of current = this.head. So the while loop never runs and count stays zero.
🧠 Conceptual
advancedWhat is the time complexity of getLength in a singly linked list?
Given a singly linked list with n nodes, what is the time complexity of the getLength method that counts nodes by traversing the list?
Attempts:
2 left
💡 Hint
The method visits each node once.
✗ Incorrect
The getLength method visits each node exactly once, so the time grows linearly with the number of nodes.
🚀 Application
expertWhat is the length of the linked list after these operations?
Starting with an empty linked list, perform these operations in order:
1. Insert 1
2. Insert 2
3. Insert 3
4. Remove the head node
5. Insert 4
What is the length of the linked list now?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; insert(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; } } removeHead() { if (this.head) { this.head = this.head.next; } } getLength(): number { let count = 0; let current = this.head; while (current) { count++; current = current.next; } return count; } } const list = new LinkedList(); list.insert(1); list.insert(2); list.insert(3); list.removeHead(); list.insert(4); console.log(list.getLength());
Attempts:
2 left
💡 Hint
Track the list nodes after each operation carefully.
✗ Incorrect
After inserting 1,2,3 the list length is 3. Removing head removes node with value 1, length becomes 2. Inserting 4 adds one node, length becomes 3.
