Bird
Raised Fist0
DSA Javaprogramming~20 mins

Remove Nth Node from End of List in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Remove Nth Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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");
    }
}
A1 -> 3 -> 4 -> 5 -> null
B1 -> 2 -> 4 -> 5 -> null
C1 -> 2 -> 3 -> 5 -> null
D2 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Remember the 2nd node from the end is the 4th node from the start in a 5-node list.
❓ Predict Output
intermediate
2:00remaining
Output 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");
    }
}
A20 -> 30 -> null
B10 -> 30 -> null
C10 -> 20 -> 30 -> null
D10 -> 20 -> null
Attempts:
2 left
💡 Hint
Removing the last node means the node with value 30 is removed.
🔧 Debug
advanced
2:00remaining
Identify 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;
    }
}
ANullPointerException
BIndexOutOfBoundsException
CNo error, works correctly
DCompilation error
Attempts:
2 left
💡 Hint
Check how many times the first pointer moves ahead before the loop.
🧠 Conceptual
advanced
1:00remaining
Why 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?
ATo simplify edge cases like removing the head node
BTo increase the speed of traversal
CTo store the length of the list
DTo avoid using two pointers
Attempts:
2 left
💡 Hint
Think about what happens if the head node itself needs to be removed.
🚀 Application
expert
3:00remaining
Resulting 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");
    }
}
A5 -> 10 -> 20 -> null
B5 -> 10 -> 25 -> null
C10 -> 15 -> 25 -> null
D5 -> 15 -> 25 -> null
Attempts:
2 left
💡 Hint
Remove the 3rd node from end twice and track the list after each removal.

Practice

(1/5)
1. What does the problem "Remove Nth Node from End of List" ask you to do with a linked list?
easy
A. Reverse the entire linked list
B. Add a new node at the Nth position from the start
C. Find the value of the Nth node from the start
D. Remove the node that is Nth from the end of the list

Solution

  1. Step 1: Understand the problem statement

    The problem requires removing the node that is Nth from the end of the linked list, not from the start.
  2. Step 2: Clarify the operation on the list

    We do not add or reverse nodes; we specifically remove one node counting from the end.
  3. Final Answer:

    Remove the node that is Nth from the end of the list -> Option D
  4. Quick Check:

    Remove Nth from end = Remove Nth from end [OK]
Hint: Focus on 'Nth from end' means counting backwards [OK]
Common Mistakes:
  • Confusing Nth from end with Nth from start
  • Thinking the problem asks to add or reverse nodes
  • Ignoring the direction of counting nodes
2. Which of the following Java method signatures correctly represents a function to remove the Nth node from the end of a singly linked list?
easy
A. public ListNode removeNthFromEnd(ListNode head, int n)
B. public void removeNthFromEnd(int n, ListNode head)
C. public ListNode removeNode(ListNode head, int index)
D. public ListNode deleteNthNode(ListNode node, int n)

Solution

  1. Step 1: Check method parameters and return type

    The method should take the head of the list and an integer n, and return the new head after removal.
  2. Step 2: Match signature with problem requirements

    public ListNode removeNthFromEnd(ListNode head, int n) matches the expected signature: it takes (ListNode head, int n) and returns ListNode.
  3. Final Answer:

    public ListNode removeNthFromEnd(ListNode head, int n) -> Option A
  4. Quick Check:

    Correct signature = public ListNode removeNthFromEnd(ListNode head, int n) [OK]
Hint: Method must return updated head and accept head, n as parameters [OK]
Common Mistakes:
  • Wrong parameter order
  • Void return type instead of ListNode
  • Using incorrect parameter types
3. What will be the output linked list after removing the 2nd node from the end in the list: 1 -> 2 -> 3 -> 4 -> 5 -> null?
medium
A. 1 -> 2 -> 4 -> 5 -> null
B. 1 -> 2 -> 3 -> 5 -> null
C. 1 -> 3 -> 4 -> 5 -> null
D. 2 -> 3 -> 4 -> 5 -> null

Solution

  1. Step 1: Identify the 2nd node from the end

    Counting from the end: 5 (1st), 4 (2nd). So node with value 4 is to be removed.
  2. Step 2: Remove node with value 4 and link previous node to next

    After removal, list becomes 1 -> 2 -> 3 -> 5 -> null.
  3. Final Answer:

    1 -> 2 -> 3 -> 5 -> null -> Option B
  4. Quick Check:

    Remove 2nd from end = Remove node 4 [OK]
Hint: Count nodes from end to find which to remove [OK]
Common Mistakes:
  • Removing wrong node counting from start
  • Off-by-one errors in counting
  • Not updating links correctly after removal
4. Given the following code snippet to remove the Nth node from the end, what is the error?
public ListNode removeNthFromEnd(ListNode head, int n) {
    ListNode fast = head;
    ListNode slow = head;
    for (int i = 0; i <= n; i++) {
        fast = fast.next;
    }
    while (fast != null) {
        fast = fast.next;
        slow = slow.next;
    }
    slow.next = slow.next.next;
    return head;
}
medium
A. The for loop should run from i = 0 to i < n, not i <= n
B. The slow pointer should start at null, not head
C. The method should return slow, not head
D. The while loop condition should be fast.next != null

Solution

  1. Step 1: Analyze the for loop for moving fast pointer

    The loop runs i from 0 to n inclusive (i <= n), moving fast n+1 steps, which is one step too many and causes NullPointerException.
  2. Step 2: Correct the loop to run i < n to move fast exactly n steps ahead

    This ensures fast is n nodes ahead of slow, allowing correct removal of the target node.
  3. Final Answer:

    The for loop should run from i = 0 to i < n, not i <= n -> Option A
  4. Quick Check:

    Loop count off by one = The for loop should run from i = 0 to i < n, not i <= n [OK]
Hint: Check loop bounds carefully for pointer steps [OK]
Common Mistakes:
  • Using i <= n instead of i < n in loop
  • Starting slow pointer incorrectly
  • Returning wrong node after removal
5. You have a linked list: 10 -> 20 -> 30 -> 40 -> 50 -> null. You want to remove the 5th node from the end. What will be the resulting list after removal?
hard
A. 30 -> 40 -> 50 -> null
B. 10 -> 20 -> 30 -> 40 -> null
C. 20 -> 30 -> 40 -> 50 -> null
D. 10 -> 20 -> 30 -> 40 -> 50 -> null

Solution

  1. Step 1: Identify the 5th node from the end

    Since the list has 5 nodes, the 5th from the end is the first node with value 10.
  2. Step 2: Remove the first node and update head

    Removing node 10 means the new head is node 20, so list becomes 20 -> 30 -> 40 -> 50 -> null.
  3. Final Answer:

    20 -> 30 -> 40 -> 50 -> null -> Option C
  4. Quick Check:

    Remove first node = 20 -> 30 -> 40 -> 50 -> null [OK]
Hint: If N equals list length, remove head node [OK]
Common Mistakes:
  • Not updating head when first node is removed
  • Removing wrong node counting from end
  • Confusing list length with N
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard