Bird
0
0
DSA Cprogramming

Pop Using Linked List Node in DSA C

Choose your learning style9 modes available
Mental Model
Pop means removing the first item from the list and returning it. We change the start pointer to the next item.
Analogy: Imagine a line of people holding hands. To pop, the first person lets go and leaves, so the second person becomes the new first.
head -> 1 -> 2 -> 3 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null, pop once
Goal: Remove the first node and update the head to the next node
Step 1: store current head node (1) to return later
head -> [1] -> 2 -> 3 -> null
Why: We keep the first node to return its value after removal
Step 2: move head pointer to next node (2)
head -> [2] -> 3 -> null
Why: The second node becomes the new first node after pop
Step 3: disconnect old head node (1) from list
[1] -> null, head -> 2 -> 3 -> null
Why: Old first node is removed from the list to avoid dangling links
Result:
head -> 2 -> 3 -> null
popped value: 1
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

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

// Function to push a new node at the front
void push(Node** head_ref, int new_data) {
    Node* new_node = (Node*)malloc(sizeof(Node));
    new_node->data = new_data;
    new_node->next = *head_ref;
    *head_ref = new_node;
}

// Function to pop the first node and return its data
int pop(Node** head_ref) {
    if (*head_ref == NULL) {
        return -1; // List empty
    }
    Node* temp = *head_ref; // store current head
    int popped_data = temp->data;
    *head_ref = temp->next; // move head to next node
    temp->next = NULL; // disconnect old head
    free(temp); // free memory
    return popped_data;
}

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

int main() {
    Node* head = NULL;
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);

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

    int popped = pop(&head);
    printf("Popped value: %d\n", popped);

    printf("List after pop: ");
    printList(head);

    return 0;
}
Node* temp = *head_ref; // store current head
store the first node to return its data later
*head_ref = temp->next; // move head to next node
update head pointer to next node to remove first node
temp->next = NULL; // disconnect old head
disconnect old head from list to avoid dangling pointer
free(temp); // free memory
release memory of removed node
OutputSuccess
Original list: 1 -> 2 -> 3 -> null Popped value: 1 List after pop: 2 -> 3 -> null
Complexity Analysis
Time: O(1) because we only remove the first node without traversing the list
Space: O(1) because no extra space is used except temporary pointer
vs Alternative: Compared to removing from the end which requires O(n) traversal, popping from front is faster and simpler
Edge Cases
empty list
pop returns -1 indicating no node to remove
DSA C
if (*head_ref == NULL) { return -1; }
single node list
pop removes the only node and head becomes NULL
DSA C
*head_ref = temp->next; // moves head to NULL
When to Use This Pattern
When you need to remove the first element from a linked list quickly, use the pop pattern to update the head pointer.
Common Mistakes
Mistake: Not updating the head pointer after removing the first node
Fix: Always assign head to head->next to keep list valid
Mistake: Not freeing the removed node's memory
Fix: Call free() on the removed node to avoid memory leaks
Summary
Removes the first node from a linked list and returns its value.
Use when you want to quickly remove the front element of a list.
The key is to move the head pointer to the next node and free the old first node.