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
Given the linked list 1 -> 2 -> 3 -> 4 -> 5 -> null, what is the output after removing the 2nd node from the end?
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class Solution { public static ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode first = dummy; ListNode second = dummy; for (int 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; } public static void main(String[] args) { ListNode head = new ListNode(1); head.next = new ListNode(2); head.next.next = new ListNode(3); head.next.next.next = new ListNode(4); head.next.next.next.next = new ListNode(5); ListNode result = removeNthFromEnd(head, 2); while (result != null) { System.out.print(result.val + " -> "); result = result.next; } System.out.print("null"); } }
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, so the list becomes 1 -> 2 -> 3 -> 5 -> null.
❓ Predict Output
intermediateOutput after removing 1st node from end
Given the linked list 10 -> 20 -> 30 -> null, what is the output after removing the 1st node from the end?
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class Solution { public static ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode first = dummy; ListNode second = dummy; for (int 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; } public static void main(String[] args) { ListNode head = new ListNode(10); head.next = new ListNode(20); head.next.next = new ListNode(30); ListNode result = removeNthFromEnd(head, 1); while (result != null) { System.out.print(result.val + " -> "); result = result.next; } System.out.print("null"); } }
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 becomes 10 -> 20 -> null.
🔧 Debug
advancedIdentify the error in removing nth node from end
What error does the following code produce when removing the 3rd node from the end of list 1 -> 2 -> 3 -> 4 -> 5 -> null?
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class Solution { public static ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode first = dummy; ListNode second = dummy; for (int i = 0; i < n; i++) { first = first.next; } while (first.next != null) { first = first.next; second = second.next; } second.next = second.next.next; return dummy.next; } }
Attempts:
2 left
💡 Hint
Check how many times the first pointer moves ahead before the loop.
✗ Incorrect
The for loop moves first pointer n times instead of n+1, so when n equals list length, first becomes null and first.next causes NullPointerException.
🧠 Conceptual
advancedWhy use a dummy node in removing nth node from end?
Why is a dummy node used at the start of the linked list when removing the nth node from the end?
Attempts:
2 left
💡 Hint
Think about what happens if the head node itself needs to be removed.
✗ Incorrect
Dummy node helps handle cases where the node to remove is the head, avoiding null pointer errors and simplifying code.
🚀 Application
expertResulting list after multiple removals
Starting with the list 5 -> 10 -> 15 -> 20 -> 25 -> null, remove the 3rd node from the end twice in a row. What is the final list?
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class Solution { public static ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode first = dummy; ListNode second = dummy; for (int 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; } public static void main(String[] args) { ListNode head = new ListNode(5); head.next = new ListNode(10); head.next.next = new ListNode(15); head.next.next.next = new ListNode(20); head.next.next.next.next = new ListNode(25); head = removeNthFromEnd(head, 3); head = removeNthFromEnd(head, 3); ListNode curr = head; while (curr != null) { System.out.print(curr.val + " -> "); curr = curr.next; } System.out.print("null"); } }
Attempts:
2 left
💡 Hint
Remove the 3rd node from end twice and track the list after each removal.
✗ Incorrect
First removal removes node with value 15 (3rd from end). List becomes 5 -> 10 -> 20 -> 25. Second removal removes node with value 20 (3rd from end now). Final list is 5 -> 10 -> 25 -> null.
