Bird
0
0
DSA Cprogramming

Push Using Linked List Node in DSA C

Choose your learning style9 modes available
Mental Model
Add a new item at the start of a linked list by making it point to the current first item.
Analogy: Imagine adding a new book on top of a stack of books; the new book becomes the first one you see.
head -> 1 -> 2 -> 3 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null, push value 0
Goal: Add 0 at the start so the list becomes 0 -> 1 -> 2 -> 3 -> null
Step 1: Create new node with value 0
new_node[0] -> null, head -> 1 -> 2 -> 3 -> null
Why: We need a new node to add to the list
Step 2: Set new_node's next to current head (node with value 1)
new_node[0] -> 1 -> 2 -> 3 -> null, head -> 1 -> 2 -> 3 -> null
Why: Link new node to the existing list so it points to the first node
Step 3: Update head to new_node
head -> 0 -> 1 -> 2 -> 3 -> null
Why: New node becomes the first node in the list
Result:
head -> 0 -> 1 -> 2 -> 3 -> null
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

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

// Push function to add node at start
void push(struct Node** head_ref, int new_data) {
    // Create new node
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = new_data;

    // Link new node to current head
    new_node->next = *head_ref;

    // Update head to new node
    *head_ref = new_node;
}

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

int main() {
    // Start with list 1->2->3->null
    struct Node* head = NULL;

    // Push 3
    push(&head, 3);
    // Push 2
    push(&head, 2);
    // Push 1
    push(&head, 1);

    // Print original list
    printList(head);

    // Push 0 at start
    push(&head, 0);

    // Print updated list
    printList(head);

    return 0;
}
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
Create new node to hold new data
new_node->next = *head_ref;
Link new node to current head node
*head_ref = new_node;
Update head pointer to new node
OutputSuccess
1 -> 2 -> 3 -> null 0 -> 1 -> 2 -> 3 -> null
Complexity Analysis
Time: O(1) because we only change pointers without traversing the list
Space: O(1) because we allocate one new node regardless of list size
vs Alternative: Compared to adding at the end which is O(n), pushing at start is faster and simpler
Edge Cases
Empty list (head is NULL)
New node becomes the only node and head points to it
DSA C
new_node->next = *head_ref;
When to Use This Pattern
When you need to add an element quickly at the start of a linked list, use push to update head pointer.
Common Mistakes
Mistake: Not updating the head pointer after creating the new node
Fix: Assign the new node to head pointer to make it the first node
Mistake: Forgetting to link new node's next to the old head
Fix: Set new_node->next to current head before updating head
Summary
Adds a new node at the beginning of a linked list.
Use when you want to insert elements quickly at the start.
The new node points to the old first node, then head updates to new node.