Challenge - 5 Problems
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after deleting node at position 2?
Given the linked list 10 -> 20 -> 30 -> 40 -> null, what is the list after deleting the node at position 2 (0-based index)?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteAtPosition(pos) { if (!this.head) return; if (pos === 0) { this.head = this.head.next; return; } let current = this.head; let prev = null; let count = 0; while (current && count < pos) { prev = current; current = current.next; count++; } if (current) { prev.next = current.next; } } toArray() { const result = []; let current = this.head; while (current) { result.push(current.data); current = current.next; } return result; } } const list = new LinkedList(); list.append(10); list.append(20); list.append(30); list.append(40); list.deleteAtPosition(2); console.log(list.toArray());
Attempts:
2 left
💡 Hint
Remember that position 2 means the third node in the list (0-based).
✗ Incorrect
Deleting node at position 2 removes the node with value 30. The list becomes 10 -> 20 -> 40 -> null.
❓ Predict Output
intermediateWhat is the output after deleting node at position 0?
Given the linked list 5 -> 15 -> 25 -> null, what is the list after deleting the node at position 0?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteAtPosition(pos) { if (!this.head) return; if (pos === 0) { this.head = this.head.next; return; } let current = this.head; let prev = null; let count = 0; while (current && count < pos) { prev = current; current = current.next; count++; } if (current) { prev.next = current.next; } } toArray() { const result = []; let current = this.head; while (current) { result.push(current.data); current = current.next; } return result; } } const list = new LinkedList(); list.append(5); list.append(15); list.append(25); list.deleteAtPosition(0); console.log(list.toArray());
Attempts:
2 left
💡 Hint
Deleting position 0 removes the first node.
✗ Incorrect
Deleting node at position 0 removes the head node with value 5. The new head is node with value 15.
❓ Predict Output
advancedWhat is the output after deleting node at position 4?
Given the linked list 1 -> 2 -> 3 -> 4 -> 5 -> null, what is the list after deleting the node at position 4?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteAtPosition(pos) { if (!this.head) return; if (pos === 0) { this.head = this.head.next; return; } let current = this.head; let prev = null; let count = 0; while (current && count < pos) { prev = current; current = current.next; count++; } if (current) { prev.next = current.next; } } toArray() { const result = []; let current = this.head; while (current) { result.push(current.data); current = current.next; } return result; } } const list = new LinkedList(); [1, 2, 3, 4, 5].forEach(n => list.append(n)); list.deleteAtPosition(4); console.log(list.toArray());
Attempts:
2 left
💡 Hint
Position 4 is the last node in this list.
✗ Incorrect
Deleting node at position 4 removes the last node with value 5. The list ends at node with value 4.
❓ Predict Output
advancedWhat happens if you delete node at position 10 in a 5-node list?
Given the linked list 100 -> 200 -> 300 -> 400 -> 500 -> null, what is the list after deleting the node at position 10?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteAtPosition(pos) { if (!this.head) return; if (pos === 0) { this.head = this.head.next; return; } let current = this.head; let prev = null; let count = 0; while (current && count < pos) { prev = current; current = current.next; count++; } if (current) { prev.next = current.next; } } toArray() { const result = []; let current = this.head; while (current) { result.push(current.data); current = current.next; } return result; } } const list = new LinkedList(); [100, 200, 300, 400, 500].forEach(n => list.append(n)); list.deleteAtPosition(10); console.log(list.toArray());
Attempts:
2 left
💡 Hint
Deleting a position beyond list length should not change the list.
✗ Incorrect
Position 10 does not exist, so no deletion happens. The list remains unchanged.
🧠 Conceptual
expertWhat is the time complexity of deleting a node at position k in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity to delete the node at position k (0-based index)?
Attempts:
2 left
💡 Hint
Think about how you reach the node before the one to delete.
✗ Incorrect
You must traverse from the head to the node before position k, which takes O(k) time. Deletion itself is O(1).
