Bird
0
0
DSA Cprogramming

Delete Node at Specific Position in DSA C

Choose your learning style9 modes available
Mental Model
We remove a node from a linked list by changing the pointer of the previous node to skip the node to delete.
Analogy: Imagine a chain of paper clips linked together. To remove one clip in the middle, you unhook the clip before it and link it directly to the clip after it.
head -> 1 -> 2 -> 3 -> 4 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> 4 -> null, delete node at position 2 (0-based)
Goal: Remove the node at position 2 so the list becomes 1 -> 2 -> 4 -> null
Step 1: Check if position is 0, if yes, remove head
head -> 1 -> 2 -> 3 -> 4 -> null
Why: We must handle deleting the first node differently
Step 2: Traverse to node before position 2 (node at position 1)
head -> 1 -> [curr->2] -> 3 -> 4 -> null
Why: We need the previous node to change its next pointer
Step 3: Change curr's next pointer to skip node at position 2
head -> 1 -> 2 -> 4 -> null
Why: Skipping the node removes it from the list
Result:
head -> 1 -> 2 -> 4 -> null
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Node structure
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// Function to create a new node
Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// Function to delete node at given position (0-based)
void deleteNodeAtPosition(Node** head_ref, int position) {
    if (*head_ref == NULL) return; // empty list

    Node* temp = *head_ref;

    // If head needs to be removed
    if (position == 0) {
        *head_ref = temp->next; // move head
        free(temp); // free old head
        return;
    }

    // Find previous node of the node to be deleted
    for (int i = 0; temp != NULL && i < position - 1; i++) {
        temp = temp->next;
    }

    // If position is more than number of nodes
    if (temp == NULL || temp->next == NULL) return;

    // Node temp->next is the node to delete
    Node* next = temp->next->next;

    free(temp->next); // free memory

    temp->next = next; // unlink deleted node
}

// Function to print the linked list
void printList(Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("null\n");
}

int main() {
    // Create list 1->2->3->4->null
    Node* head = createNode(1);
    head->next = createNode(2);
    head->next->next = createNode(3);
    head->next->next->next = createNode(4);

    printf("Original list:\n");
    printList(head);

    // Delete node at position 2
    deleteNodeAtPosition(&head, 2);

    printf("List after deleting node at position 2:\n");
    printList(head);

    return 0;
}
if (position == 0) {
Check if head node needs to be deleted
*head_ref = temp->next;
Move head pointer to next node to remove first node
for (int i = 0; temp != NULL && i < position - 1; i++) { temp = temp->next; }
Traverse to node before the one to delete
if (temp == NULL || temp->next == NULL) return;
Check if position is valid
Node* next = temp->next->next;
Store pointer to node after the one to delete
free(temp->next);
Free memory of node to delete
temp->next = next;
Link previous node to node after deleted node
OutputSuccess
Original list: 1 -> 2 -> 3 -> 4 -> null List after deleting node at position 2: 1 -> 2 -> 4 -> null
Complexity Analysis
Time: O(n) because we may need to traverse up to n nodes to reach the position
Space: O(1) because we only use a few pointers and no extra data structures
vs Alternative: Compared to rebuilding the list or copying nodes, this method deletes in place with no extra space and linear time
Edge Cases
Empty list
Function returns immediately without changes
DSA C
if (*head_ref == NULL) return;
Delete head node (position 0)
Head pointer moves to next node, old head freed
DSA C
if (position == 0) { *head_ref = temp->next; free(temp); return; }
Position greater than list length
Function returns without changes
DSA C
if (temp == NULL || temp->next == NULL) return;
When to Use This Pattern
When asked to remove a node at a specific position in a linked list, use pointer traversal to find the previous node and adjust links to exclude the target node.
Common Mistakes
Mistake: Not handling deletion of the head node separately
Fix: Add a condition to check if position is 0 and update head pointer accordingly
Mistake: Not checking if position is out of bounds causing null pointer errors
Fix: Add checks to ensure the node before the target and the target node exist before deletion
Summary
Deletes a node at a given position in a singly linked list by adjusting pointers.
Use when you need to remove an element from anywhere in a linked list by position.
The key is to find the node before the target and link it to the node after the target to remove it.