Bird
Raised Fist0
DSA Cprogramming~20 mins

Remove Nth Node from End of List in DSA C - 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
What is the output linked list after removing the 2nd node from the end in the given list?
DSA C
struct Node {
    int val;
    struct Node* next;
};

// List: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
// Remove 2nd node from end

struct Node* removeNthFromEnd(struct Node* head, int n) {
    struct Node* fast = head;
    struct Node* slow = head;
    for (int i = 0; i < n; i++) {
        fast = fast->next;
    }
    if (!fast) {
        struct Node* temp = head;
        head = head->next;
        free(temp);
        return head;
    }
    while (fast->next) {
        fast = fast->next;
        slow = slow->next;
    }
    struct Node* temp = slow->next;
    slow->next = slow->next->next;
    free(temp);
    return head;
}
A1 -> 3 -> 4 -> 5 -> NULL
B2 -> 3 -> 4 -> 5 -> NULL
C1 -> 2 -> 3 -> 5 -> NULL
D1 -> 2 -> 4 -> 5 -> NULL
Attempts:
2 left
💡 Hint
Remember the 2nd node from the end is the node before the last node.
❓ Predict Output
intermediate
2:00remaining
Output after removing 1st node from end
What is the output linked list after removing the last node (1st from end) from the list?
DSA C
struct Node {
    int val;
    struct Node* next;
};

// List: 10 -> 20 -> 30 -> NULL
// Remove 1st node from end

struct Node* removeNthFromEnd(struct Node* head, int n) {
    struct Node* fast = head;
    struct Node* slow = head;
    for (int i = 0; i < n; i++) {
        fast = fast->next;
    }
    if (!fast) {
        struct Node* temp = head;
        head = head->next;
        free(temp);
        return head;
    }
    while (fast->next) {
        fast = fast->next;
        slow = slow->next;
    }
    struct Node* temp = slow->next;
    slow->next = slow->next->next;
    free(temp);
    return head;
}
A10 -> 20 -> NULL
B10 -> 20 -> 30 -> NULL
C20 -> 30 -> NULL
D10 -> 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 will this code produce when removing the 3rd node from end in a 2-node list?
DSA C
struct Node* removeNthFromEnd(struct Node* head, int n) {
    struct Node* fast = head;
    struct Node* slow = head;
    for (int i = 0; i < n; i++) {
        fast = fast->next;
    }
    if (!fast) {
        struct Node* temp = head;
        head = head->next;
        free(temp);
        return head;
    }
    while (fast->next) {
        fast = fast->next;
        slow = slow->next;
    }
    struct Node* temp = slow->next;
    slow->next = slow->next->next;
    free(temp);
    return head;
}

// List: 1 -> 2 -> NULL
// Remove 3rd node from end
ANo error, returns 2 -> NULL
BSegmentation fault (accessing NULL pointer)
CMemory leak error
DCompilation error due to missing return
Attempts:
2 left
💡 Hint
Check what happens when fast pointer moves beyond list length.
🧠 Conceptual
advanced
2:00remaining
Why use two pointers in removing nth node from end?
Why does the two-pointer technique work for removing the nth node from the end of a linked list?
ABecause two pointers can traverse the list twice faster
BBecause two pointers avoid using extra memory for storing nodes
CBecause one pointer is used to reverse the list while the other deletes nodes
DBecause moving one pointer n steps ahead creates a gap to find the target node in one pass
Attempts:
2 left
💡 Hint
Think about how the gap helps locate the node to remove.
❓ Predict Output
expert
3:00remaining
Output after removing 4th node from end in 5-node list
Given the list 5 -> 10 -> 15 -> 20 -> 25 -> NULL, what is the output after removing the 4th node from the end?
DSA C
struct Node* removeNthFromEnd(struct Node* head, int n) {
    struct Node* fast = head;
    struct Node* slow = head;
    for (int i = 0; i < n; i++) {
        fast = fast->next;
    }
    if (!fast) {
        struct Node* temp = head;
        head = head->next;
        free(temp);
        return head;
    }
    while (fast->next) {
        fast = fast->next;
        slow = slow->next;
    }
    struct Node* temp = slow->next;
    slow->next = slow->next->next;
    free(temp);
    return head;
}

// Remove 4th node from end
A5 -> 15 -> 20 -> 25 -> NULL
B10 -> 15 -> 20 -> 25 -> NULL
C5 -> 10 -> 20 -> 25 -> NULL
D5 -> 10 -> 15 -> 25 -> NULL
Attempts:
2 left
💡 Hint
Count nodes from end to find which node is removed.

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. Remove the node that is Nth from the end of the list
B. Remove the node that is Nth from the start of the list
C. Add a new node at the Nth position from the end
D. Reverse the linked list up to the Nth node from the end

Solution

  1. Step 1: Understand the problem statement

    The problem requires removing the node that is exactly N positions from the end of the linked list.
  2. Step 2: Clarify the difference from other options

    Options A, C, and D describe different operations: removing from start, adding nodes, or reversing, which are not asked here.
  3. Final Answer:

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

    Remove Nth from end = Remove the node that is Nth from the end of the list [OK]
Hint: Focus on 'Nth from end' means counting backward [OK]
Common Mistakes:
  • Confusing Nth from start with Nth from end
  • Thinking the problem asks to add nodes
  • Assuming the list needs reversing
2. Which of the following is the correct way to declare a pointer to the head of a singly linked list in C?
easy
A. struct Node head*;
B. Node head*;
C. struct Node* head;
D. Node* head;

Solution

  1. Step 1: Recall C struct pointer syntax

    In C, to declare a pointer to a struct Node, use 'struct Node* head;'.
  2. Step 2: Identify incorrect syntax

    Options A, B, and D misuse the order or omit 'struct' keyword which is required unless typedef is used.
  3. Final Answer:

    struct Node* head; -> Option C
  4. Quick Check:

    Pointer to struct Node = struct Node* head; [OK]
Hint: Remember 'struct' keyword before struct name in C [OK]
Common Mistakes:
  • Omitting 'struct' keyword in declaration
  • Placing '*' incorrectly after variable name
  • Using typedef name without defining it
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. 2 -> 3 -> 4 -> 5 -> null
C. 1 -> 3 -> 4 -> 5 -> null
D. 1 -> 2 -> 3 -> 5 -> null

Solution

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

    The list is 1->2->3->4->5. Counting from 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 to next

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

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

    Remove 2nd from end (4) = 1->2->3->5->null [OK]
Hint: Count nodes from end to find which to remove [OK]
Common Mistakes:
  • Removing the wrong node counting from start
  • Skipping the link update after removal
  • Confusing 2nd from end with 2nd from start
4. Given the following code snippet to remove the Nth node from end, what is the error?
struct Node* removeNthFromEnd(struct Node* head, int n) {
    struct Node* fast = head;
    struct Node* slow = head;
    for (int i = 0; i < n; i++) {
        fast = fast->next;
    }
    while (fast->next != NULL) {
        fast = fast->next;
        slow = slow->next;
    }
    slow->next = slow->next->next;
    return head;
}
medium
A. The slow pointer is moved too many times causing segmentation fault
B. The loop does not check if fast becomes NULL before accessing fast->next
C. The function does not handle the case when n is zero
D. The function returns the wrong pointer instead of head

Solution

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

    The loop moves fast pointer n times without checking if fast is NULL, which can cause a crash if n is larger than list length.
  2. Step 2: Check while loop condition and pointer usage

    The while loop uses fast->next without null check before the loop, which is unsafe if fast is NULL.
  3. Final Answer:

    The loop does not check if fast becomes NULL before accessing fast->next -> Option B
  4. Quick Check:

    Missing NULL check on fast pointer causes error [OK]
Hint: Always check pointers for NULL before dereferencing [OK]
Common Mistakes:
  • Not checking if fast is NULL before fast->next
  • Assuming list length is always >= n
  • Ignoring edge case when removing head node
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?
hard
A. 20 -> 30 -> 40 -> 50 -> null
B. 10 -> 20 -> 30 -> 40 -> null
C. 10 -> 20 -> 30 -> 40 -> 50 -> null (no change)
D. 30 -> 40 -> 50 -> null

Solution

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

    The list length is 5, so the 5th node from the end is the first node with value 10.
  2. Step 2: Remove the head node and update head pointer

    Removing the first node results in the list: 20 -> 30 -> 40 -> 50 -> null.
  3. Final Answer:

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

    Remove 5th from end (head) = list without first node [OK]
Hint: Removing N equals list length means remove head [OK]
Common Mistakes:
  • Not updating head pointer when removing first node
  • Confusing Nth from end with Nth from start
  • Assuming no change if N equals list length
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