Bird
0
0
DSA Cprogramming

Delete Node at Beginning in DSA C

Choose your learning style9 modes available
Mental Model
To remove the first item from a linked list, just move the start pointer to the next item and forget the old first one.
Analogy: Imagine a line of people holding hands. To remove the first person, you just let go of their hand and start holding the next person's hand instead.
head -> 1 -> 2 -> 3 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3, delete node at beginning
Goal: Remove the first node and update the list to start from the second node
Step 1: store the current head node (1) to delete later
head -> [1] -> 2 -> 3 -> null
Why: We keep the first node temporarily to free its memory after updating head
Step 2: move head pointer to the next node (2)
head -> 2 -> 3 -> null
Why: This makes the second node the new first node in the list
Step 3: free the old first node (1)
head -> 2 -> 3 -> null
Why: We remove the old first node from memory to avoid leaks
Result:
head -> 2 -> 3 -> null
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

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

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

// Function to delete node at beginning
void deleteAtBeginning(struct Node** head_ref) {
    if (*head_ref == NULL) return; // empty list guard

    struct Node* temp = *head_ref; // store current head
    *head_ref = temp->next;        // move head to next node
    free(temp);                    // free old head
}

// Driver code
int main() {
    // Create list 1->2->3->null
    struct Node* head = malloc(sizeof(struct Node));
    head->data = 1;
    head->next = malloc(sizeof(struct Node));
    head->next->data = 2;
    head->next->next = malloc(sizeof(struct Node));
    head->next->next->data = 3;
    head->next->next->next = NULL;

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

    deleteAtBeginning(&head);

    printf("After deleting node at beginning:\n");
    printList(head);

    // Free remaining nodes
    while (head != NULL) {
        struct Node* temp = head;
        head = head->next;
        free(temp);
    }

    return 0;
}
if (*head_ref == NULL) return; // empty list guard
check if list is empty to avoid errors
struct Node* temp = *head_ref; // store current head
keep reference to old first node to free later
*head_ref = temp->next; // move head to next node
update head pointer to second node
free(temp); // free old head
release memory of removed node
OutputSuccess
Original list: 1 -> 2 -> 3 -> null After deleting node at beginning: 2 -> 3 -> null
Complexity Analysis
Time: O(1) because we only update pointers once without traversing the list
Space: O(1) because no extra space is used besides a temporary pointer
vs Alternative: Compared to deleting at the end which requires O(n) traversal, deleting at beginning is faster and simpler
Edge Cases
empty list
function returns immediately without changes
DSA C
if (*head_ref == NULL) return; // empty list guard
single node list
head becomes NULL after deletion, list becomes empty
DSA C
*head_ref = temp->next;        // move head to next node
When to Use This Pattern
When you need to remove the first item from a linked list quickly, use the delete at beginning pattern because it only changes one pointer.
Common Mistakes
Mistake: Not updating the head pointer after deletion
Fix: Always assign head to head->next to move the start of the list
Mistake: Not checking if the list is empty before deleting
Fix: Add a guard to return immediately if head is NULL
Summary
Deletes the first node of a linked list by moving the head pointer to the next node.
Use when you want to remove the earliest element quickly without traversing the list.
The key is to update the head pointer and free the old first node to avoid memory leaks.