Bird
Raised Fist0
DSA Cprogramming~10 mins

Delete Node at Beginning in DSA C - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete Node at Beginning
Start
↓
Check if list is empty?
Yes→Nothing to delete, EXIT
No↓
Save pointer to first node
↓
Move head to second node
↓
Free memory of old first node
↓
Return updated list
↓
END
This flow checks if the list is empty, then removes the first node by moving the head pointer and freeing the old node.
Execution Sample
DSA C
struct Node {
    int data;
    struct Node* next;
};

void deleteAtBeginning(struct Node** head) {
    if (*head == NULL) return;
    struct Node* temp = *head;
    *head = (*head)->next;
    free(temp);
}
This code deletes the first node of a singly linked list by updating the head pointer and freeing the old first node.
Execution Table
StepCondition (*head == NULL)ActionHead Node DataList State
1FalseSave temp = head (Node with data 10)1010 -> 20 -> 30 -> NULL
2N/AMove head to next node (20)2020 -> 30 -> NULL
3N/AFree old first node (10)2020 -> 30 -> NULL
4N/AReturn updated list2020 -> 30 -> NULL
5N/AExit2020 -> 30 -> NULL
💡 Head moved to second node; old first node freed; list updated.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
*head10 -> 20 -> 30 -> NULL10 -> 20 -> 30 -> NULL20 -> 30 -> NULL20 -> 30 -> NULL20 -> 30 -> NULL
tempN/A10 -> 20 -> 30 -> NULL10 -> 20 -> 30 -> NULLFreedFreed
Key Moments - 3 Insights
Why do we check if the list is empty before deleting?
Because if the list is empty (*head == NULL), there is no node to delete. The execution_table row 1 shows this check prevents errors.
Why do we save the first node in a temporary variable before moving the head?
We save it to free its memory later. If we move head first, we lose reference to the old node and can't free it. See execution_table steps 1 and 3.
What happens to the list after deleting the first node?
The head points to the second node, effectively removing the first node from the list. The list state changes from '10 -> 20 -> 30' to '20 -> 30' as shown in execution_table steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the head node data after step 2?
A20
B10
C30
DNULL
💡 Hint
Check the 'Head Node Data' column at step 2 in the execution_table.
At which step is the old first node freed?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the action mentioning 'Free old first node' in the execution_table.
If the list was empty initially, what would happen at step 1?
AThe function would free a node
BThe function would return immediately without changes
CThe function would move head to next node
DThe function would crash
💡 Hint
Refer to the condition check '*head == NULL' in execution_table step 1.
Concept Snapshot
Delete Node at Beginning:
- Check if list is empty (*head == NULL)
- Save first node in temp
- Move head to second node
- Free old first node
- Updated list starts from new head
Full Transcript
This concept shows how to delete the first node of a singly linked list in C. First, we check if the list is empty to avoid errors. If not empty, we save the first node in a temporary pointer. Then, we move the head pointer to the second node. After that, we free the memory of the old first node to avoid memory leaks. Finally, the list starts from the new head node. The execution table traces these steps with the list initially containing nodes with data 10, 20, and 30. After deletion, the list starts at 20. The variable tracker shows how the head pointer and temp variable change during execution. Key moments clarify why each step is important. The visual quiz tests understanding of the head pointer's value and the freeing step.

Practice

(1/5)
1. What happens to the linked list when you delete the node at the beginning?
easy
A. All nodes are deleted at once.
B. The last node is deleted instead of the first.
C. The list remains unchanged.
D. The head pointer moves to the next node, removing the first node.

Solution

  1. Step 1: Understand the head pointer role

    The head pointer points to the first node in the linked list.
  2. Step 2: Effect of deleting the first node

    Deleting the first node means moving the head pointer to the second node, effectively removing the first node from the list.
  3. Final Answer:

    The head pointer moves to the next node, removing the first node. -> Option D
  4. Quick Check:

    Deleting at beginning means head moves forward [OK]
Hint: Deleting at start moves head to next node [OK]
Common Mistakes:
  • Thinking the last node is deleted
  • Assuming the list stays the same
  • Deleting all nodes instead of one
2. Which of the following is the correct syntax to delete the first node in a singly linked list in C?
easy
A. temp = head; head = head->next; free(temp);
B. head = head->next; free(head);
C. free(head); head = head->next;
D. head->next = NULL; free(head);

Solution

  1. Step 1: Store current head in temp

    We save the current head node in a temporary pointer to free it later.
  2. Step 2: Move head to next node and free old head

    Update head to point to the next node, then free the old head stored in temp.
  3. Final Answer:

    temp = head; head = head->next; free(temp); -> Option A
  4. Quick Check:

    Save old head, move head, free old [OK]
Hint: Save old head before moving and freeing [OK]
Common Mistakes:
  • Freeing head before moving pointer
  • Not saving old head before moving
  • Setting next to NULL incorrectly
3. What will be the output linked list after deleting the first node from the list: 10 -> 20 -> 30 -> NULL?
medium
A. 10 -> 20 -> 30 -> NULL
B. NULL
C. 20 -> 30 -> NULL
D. 30 -> NULL

Solution

  1. Step 1: Identify the first node

    The first node has value 10.
  2. Step 2: Delete first node and move head

    After deletion, head points to node with value 20, so list starts from 20.
  3. Final Answer:

    20 -> 30 -> NULL -> Option C
  4. Quick Check:

    First node removed, next node becomes head [OK]
Hint: After deletion, list starts from second node [OK]
Common Mistakes:
  • Not updating head pointer
  • Deleting wrong node
  • Assuming list becomes empty
4. Consider this code snippet to delete the first node:
struct Node* temp = head;
head = head->next;
free(head);
What is the error in this code?
medium
A. It frees the new head instead of the old head.
B. It does not update the head pointer.
C. It frees the temp pointer instead of head.
D. It causes a syntax error.

Solution

  1. Step 1: Analyze pointer usage

    temp stores old head, head moves to next node.
  2. Step 2: Identify which node is freed

    free(head) frees the new head, not the old node stored in temp, causing memory error.
  3. Final Answer:

    It frees the new head instead of the old head. -> Option A
  4. Quick Check:

    Free old node, not new head [OK]
Hint: Free the saved old head pointer, not the updated head [OK]
Common Mistakes:
  • Freeing updated head instead of old node
  • Not freeing any node causing memory leak
  • Confusing temp and head pointers
5. You have a singly linked list with nodes: 5 -> 15 -> 25 -> 35 -> NULL. After deleting the first node twice, what will be the resulting list?
hard
A. 15 -> 25 -> 35 -> NULL
B. 25 -> 35 -> NULL
C. 35 -> NULL
D. NULL

Solution

  1. Step 1: Delete first node once

    First deletion removes 5, list becomes 15 -> 25 -> 35 -> NULL.
  2. Step 2: Delete first node again

    Second deletion removes 15, list becomes 25 -> 35 -> NULL.
  3. Final Answer:

    25 -> 35 -> NULL -> Option B
  4. Quick Check:

    Two deletions remove first two nodes [OK]
Hint: Each deletion moves head forward by one node [OK]
Common Mistakes:
  • Deleting only once instead of twice
  • Confusing node values after deletion
  • Assuming list becomes empty too soon
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard