Bird
0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 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.