Challenge - 5 Problems
Remove Nth Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after removing 2nd node from end
What is the printed linked list after removing the 2nd node from the end in the given list?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { let dummy = new ListNode(0, head); let first: ListNode | null = dummy; let second: ListNode | null = dummy; for (let i = 0; i <= n; i++) { if (first) first = first.next; } while (first !== null) { first = first.next; if (second) second = second.next; } if (second && second.next) { second.next = second.next.next; } return dummy.next; } // Create linked list: 1 -> 2 -> 3 -> 4 -> 5 -> null let head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))); let newHead = removeNthFromEnd(head, 2); // Function to print list function printList(node: ListNode | null): string { let result = []; while (node !== null) { result.push(node.val); node = node.next; } return result.join(' -> ') + ' -> null'; } console.log(printList(newHead));
Attempts:
2 left
💡 Hint
Remember the 2nd node from the end is the 4th node from the start in a 5-node list.
✗ Incorrect
Removing the 2nd node from the end removes the node with value 4. The resulting list is 1 -> 2 -> 3 -> 5 -> null.
❓ Predict Output
intermediateOutput after removing 1st node from end
What is the printed linked list after removing the last node (1st from end) from the list?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { let dummy = new ListNode(0, head); let first: ListNode | null = dummy; let second: ListNode | null = dummy; for (let i = 0; i <= n; i++) { if (first) first = first.next; } while (first !== null) { first = first.next; if (second) second = second.next; } if (second && second.next) { second.next = second.next.next; } return dummy.next; } // Create linked list: 10 -> 20 -> 30 -> null let head = new ListNode(10, new ListNode(20, new ListNode(30))); let newHead = removeNthFromEnd(head, 1); function printList(node: ListNode | null): string { let result = []; while (node !== null) { result.push(node.val); node = node.next; } return result.join(' -> ') + ' -> null'; } console.log(printList(newHead));
Attempts:
2 left
💡 Hint
Removing the last node means the node with value 30 is removed.
✗ Incorrect
The last node (value 30) is removed, so the list ends at 20. The resulting list is 10 -> 20 -> null.
🔧 Debug
advancedIdentify the error in the removeNthFromEnd implementation
What error will this code produce when removing the 3rd node from the end in a 3-node list?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { let dummy = new ListNode(0, head); let first: ListNode | null = dummy; let second: ListNode | null = dummy; for (let i = 0; i <= n; i++) { first = first!.next!; } while (first !== null) { first = first.next; second = second!.next; } second!.next = second!.next!.next; return dummy.next; } // Create linked list: 1 -> 2 -> 3 -> null let head = new ListNode(1, new ListNode(2, new ListNode(3))); let newHead = removeNthFromEnd(head, 3); function printList(node: ListNode | null): string { let result = []; while (node !== null) { result.push(node.val); node = node.next; } return result.join(' -> ') + ' -> null'; } console.log(printList(newHead));
Attempts:
2 left
💡 Hint
Check the for loop and how many times first pointer moves.
✗ Incorrect
The for loop moves first pointer n times without checking if it becomes null. For n=3 in a 3-node list, first becomes null, then the while loop tries to access first.next causing TypeError.
❓ Predict Output
advancedResult after removing 4th node from end in 5-node list
What is the printed linked list after removing the 4th node from the end in the list 10 -> 20 -> 30 -> 40 -> 50 -> null?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { let dummy = new ListNode(0, head); let first: ListNode | null = dummy; let second: ListNode | null = dummy; for (let i = 0; i <= n; i++) { first = first!.next; } while (first !== null) { first = first.next; second = second!.next; } second!.next = second!.next!.next; return dummy.next; } // Create linked list: 10 -> 20 -> 30 -> 40 -> 50 -> null let head = new ListNode(10, new ListNode(20, new ListNode(30, new ListNode(40, new ListNode(50))))); let newHead = removeNthFromEnd(head, 4); function printList(node: ListNode | null): string { let result = []; while (node !== null) { result.push(node.val); node = node.next; } return result.join(' -> ') + ' -> null'; } console.log(printList(newHead));
Attempts:
2 left
💡 Hint
The 4th node from the end is the 2nd node from the start.
✗ Incorrect
Removing the 4th node from the end removes the node with value 20. The resulting list is 10 -> 30 -> 40 -> 50 -> null.
🧠 Conceptual
expertWhy use a dummy node in Remove Nth Node from End?
Why is a dummy node often used at the start of the linked list in the Remove Nth Node from End algorithm?
Attempts:
2 left
💡 Hint
Think about what happens if the node to remove is the first node.
✗ Incorrect
The dummy node acts as a fixed node before the head, so even if the head is removed, the algorithm can easily update the list without special cases.
