Bird
0
0
DSA Cprogramming

Delete from Beginning of Doubly Linked List in DSA C

Choose your learning style9 modes available
Mental Model
To remove the first item, we move the start pointer to the next item and fix links so the list stays connected.
Analogy: Imagine a train where you remove the first carriage and connect the engine directly to the second carriage.
null ← 1 ↔ 2 ↔ 3 ↔ 4 -> null
↑start
Dry Run Walkthrough
Input: list: 1 ↔ 2 ↔ 3 ↔ 4, delete from beginning
Goal: Remove the first node and update the list to start from the second node
Step 1: store pointer to first node (1) to delete
null ← [1] ↔ 2 ↔ 3 ↔ 4 -> null
↑start
Why: We need to remember the node to free it later
Step 2: move start pointer to second node (2)
null ← 1 ↔ [2] ↔ 3 ↔ 4 -> null
↑start
Why: The list should now start from the second node
Step 3: set previous pointer of new start (2) to null
null ← [2] ↔ 3 ↔ 4 -> null
↑start
Why: First node has no previous node
Step 4: free the old first node (1)
null ← 2 ↔ 3 ↔ 4 -> null
↑start
Why: Remove the old node from memory
Result:
null ← 2 ↔ 3 ↔ 4 -> null
↑start
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Node structure for doubly linked list
typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

// Function to delete from beginning
void deleteFromBeginning(Node** start) {
    if (*start == NULL) return; // empty list check

    Node* toDelete = *start; // remember first node
    *start = toDelete->next; // move start to second node

    if (*start != NULL) {
        (*start)->prev = NULL; // new start has no previous
    }

    free(toDelete); // free old first node
}

// Helper to print list
void printList(Node* start) {
    Node* curr = start;
    while (curr != NULL) {
        printf("%d", curr->data);
        if (curr->next != NULL) printf(" ↔ ");
        curr = curr->next;
    }
    printf(" -> null\n");
}

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

int main() {
    // Create list 1234
    Node* start = newNode(1);
    Node* second = newNode(2);
    Node* third = newNode(3);
    Node* fourth = newNode(4);

    start->next = second;
    second->prev = start;
    second->next = third;
    third->prev = second;
    third->next = fourth;
    fourth->prev = third;

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

    deleteFromBeginning(&start);

    printf("After deleting from beginning:\n");
    printList(start);

    return 0;
}
if (*start == NULL) return; // empty list check
check if list is empty to avoid errors
Node* toDelete = *start; // remember first node
store pointer to first node to delete later
*start = toDelete->next; // move start to second node
advance start pointer to second node
if (*start != NULL) { (*start)->prev = NULL; }
set new start's prev pointer to null
free(toDelete); // free old first node
release memory of removed node
OutputSuccess
Original list: 1 ↔ 2 ↔ 3 ↔ 4 -> null After deleting from beginning: 2 ↔ 3 ↔ 4 -> null
Complexity Analysis
Time: O(1) because we only change a few pointers without traversing the list
Space: O(1) because no extra space is used besides a few pointers
vs Alternative: Compared to deleting from the end which may require traversal, deleting from beginning is faster and simpler
Edge Cases
empty list
function returns immediately without changes
DSA C
if (*start == NULL) return; // empty list check
list with one node
start becomes NULL after deletion, list becomes empty
DSA C
*start = toDelete->next; // move start to second node
When to Use This Pattern
When you need to remove the first item quickly from a doubly linked list, use this pattern to update pointers safely and efficiently.
Common Mistakes
Mistake: Not setting the new start node's prev pointer to NULL after deletion
Fix: Always set (*start)->prev = NULL if the new start is not NULL
Mistake: Not checking if the list is empty before deleting
Fix: Add a check if (*start == NULL) before proceeding
Summary
Deletes the first node of a doubly linked list by moving the start pointer and fixing links.
Use when you want to remove the front element quickly from a doubly linked list.
The key is to update the start pointer and set the new first node's previous pointer to null.