Challenge - 5 Problems
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting node at position 3 in a singly linked list
Given the linked list 10 -> 20 -> 30 -> 40 -> 50 -> null, what is the output after deleting the node at position 3?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } function deleteNodeAtPosition(head: Node | null, position: number): Node | null { if (!head) return null; if (position === 1) return head.next; let current = head; for (let i = 1; current !== null && i < position - 1; i++) { current = current.next; } if (current === null || current.next === null) return head; current.next = current.next.next; return head; } // Create list const head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); head.next.next.next = new Node(40); head.next.next.next.next = new Node(50); const newHead = deleteNodeAtPosition(head, 3); // Function to print list function printList(head: Node | null): string { let result = ''; let current = head; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; return result; } console.log(printList(newHead));
Attempts:
2 left
💡 Hint
Remember that position 1 means the head node. Deleting at position 3 removes the third node.
✗ Incorrect
Deleting the node at position 3 removes the node with value 30. The list links 20 directly to 40 after deletion.
❓ Predict Output
intermediateOutput after deleting node at position 1 (head) in a singly linked list
Given the linked list 5 -> 15 -> 25 -> null, what is the output after deleting the node at position 1?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } function deleteNodeAtPosition(head: Node | null, position: number): Node | null { if (!head) return null; if (position === 1) return head.next; let current = head; for (let i = 1; current !== null && i < position - 1; i++) { current = current.next; } if (current === null || current.next === null) return head; current.next = current.next.next; return head; } // Create list const head = new Node(5); head.next = new Node(15); head.next.next = new Node(25); const newHead = deleteNodeAtPosition(head, 1); // Function to print list function printList(head: Node | null): string { let result = ''; let current = head; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; return result; } console.log(printList(newHead));
Attempts:
2 left
💡 Hint
Deleting position 1 means removing the head node.
✗ Incorrect
Removing the head node (5) leaves the list starting from 15.
🔧 Debug
advancedIdentify the error in this delete node function
What error will this TypeScript code raise when deleting a node at position 4 in a list of length 3?
DSA Typescript
function deleteNodeAtPosition(head: Node | null, position: number): Node | null {
if (!head) return null;
if (position === 1) return head.next;
let current = head;
for (let i = 1; i < position - 1; i++) {
current = current.next!;
}
current.next = current.next!.next;
return head;
}Attempts:
2 left
💡 Hint
Consider what happens when position is greater than the list length.
✗ Incorrect
The code assumes current.next is not null, but at position 4 in a 3-node list, current.next is null, so accessing current.next.next causes a TypeError.
🧠 Conceptual
advancedEffect of deleting a node at a position greater than list length
What happens if you try to delete a node at position 10 in a singly linked list of length 5 using a standard deleteNodeAtPosition function?
Attempts:
2 left
💡 Hint
Think about how the function checks for position validity.
✗ Incorrect
The function usually checks if the position is valid; if not, it returns the original list unchanged.
🚀 Application
expertResulting list after multiple deletions at specific positions
Starting with the list 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null, delete nodes at positions 2, then 4 (positions are relative to the current list after each deletion). What is the final list?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } function deleteNodeAtPosition(head: Node | null, position: number): Node | null { if (!head) return null; if (position === 1) return head.next; let current = head; for (let i = 1; current !== null && i < position - 1; i++) { current = current.next; } if (current === null || current.next === null) return head; current.next = current.next.next; return head; } // Create list const head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5); head.next.next.next.next.next = new Node(6); const afterFirstDeletion = deleteNodeAtPosition(head, 2); const afterSecondDeletion = deleteNodeAtPosition(afterFirstDeletion, 4); // Function to print list function printList(head: Node | null): string { let result = ''; let current = head; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; return result; } console.log(printList(afterSecondDeletion));
Attempts:
2 left
💡 Hint
Delete position 2 first, then delete position 4 on the updated list.
✗ Incorrect
After deleting position 2, list is 1 -> 3 -> 4 -> 5 -> 6 -> null. Then deleting position 4 removes node with value 5, resulting in 1 -> 3 -> 4 -> 6 -> null.
