Bird
Raised Fist0
DSA Cprogramming~10 mins

Delete from Beginning of Doubly Linked List in DSA C - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete from Beginning of Doubly Linked List
Start at head
↓
Check if head is NULL?
Yes→List empty, nothing to delete
No↓
Move head to head->next
↓
If new head not NULL, set new head->prev = NULL
↓
Delete old head node
↓
Done
Start at the head node, check if list is empty. If not, move head to next node, update pointers, delete old head.
Execution Sample
DSA C
void deleteFromBeginning() {
  if (head == NULL) return;
  Node* temp = head;
  head = head->next;
  if (head != NULL) head->prev = NULL;
  free(temp);
}
Deletes the first node of a doubly linked list by moving head and updating pointers.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with list10 <-> 20 <-> 30 <-> NULLhead → node with 10ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:10│←── │ prev:20│ │ next:20│──→ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
2Check if head is NULL10 <-> 20 <-> 30 <-> NULLhead not NULL, continueSame as step 1
3Set temp = head10 <-> 20 <-> 30 <-> NULLtemp → node with 10Same as step 1
4Move head to head->next20 <-> 30 <-> NULLhead → node with 20ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:10│←── │ prev:20│←── │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
5Set new head->prev = NULL20 <-> 30 <-> NULLhead->prev = NULLā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│←── │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6Free old head (node with 10)20 <-> 30 <-> NULLtemp freedā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│←── │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
7End20 <-> 30 <-> NULLhead points to node with 20Final list state as step 6
šŸ’” Execution stops after deleting the first node and updating head pointer.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
headnode with 10node with 10node with 20node with 20node with 20
tempNULLnode with 10node with 10node with 10freed
head->prevNULLNULLnode with 10NULLNULL
Key Moments - 3 Insights
Why do we set head->prev = NULL after moving head to next node?
Because the new head is now the first node, it should not have a previous node. This is shown in step 5 of the execution_table where head->prev changes from node with 10 to NULL.
What happens if the list is empty when we try to delete from beginning?
The function checks if head is NULL at step 2. If it is NULL, it returns immediately without doing anything, so no deletion occurs.
Why do we free the old head node after moving head pointer?
We free the old head to release memory and avoid memory leaks. This is done in step 6 after head moves to the next node.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of head after step 4?
ANode with data 10
BNode with data 20
CNULL
DNode with data 30
šŸ’” Hint
Check the 'Pointer Changes' column at step 4 in execution_table.
At which step does head->prev get set to NULL?
AStep 5
BStep 3
CStep 2
DStep 6
šŸ’” Hint
Look at the 'Operation' and 'Pointer Changes' columns in execution_table.
If the list was empty initially, what would happen at step 2?
Ahead moves to next node
Btemp is set to head
CFunction returns immediately
DOld head node is freed
šŸ’” Hint
Refer to the 'Check if head is NULL' operation in execution_table step 2.
Concept Snapshot
Delete from Beginning of Doubly Linked List:
- Check if list is empty (head == NULL)
- Save current head in temp
- Move head to head->next
- If new head exists, set head->prev = NULL
- Free old head node
- List size decreases by one
- Handles empty list safely
Full Transcript
This visual trace shows how to delete the first node from a doubly linked list. We start by checking if the list is empty. If not, we save the current head node in a temporary pointer. Then we move the head pointer to the next node. If the new head is not null, we set its previous pointer to null because it is now the first node. Finally, we free the old head node to release memory. The execution table shows each step with the list nodes and pointer changes. The variable tracker follows the head and temp pointers and the head's previous pointer. Key moments clarify why we update head->prev and why we free the old node. The quiz tests understanding of pointer updates and empty list handling. This method safely removes the first node and updates the doubly linked list structure.

Practice

(1/5)
1. What happens to the head pointer when you delete the first node from a doubly linked list?
easy
A. It remains unchanged
B. It becomes NULL immediately
C. It moves to the second node in the list
D. It points to the last node

Solution

  1. Step 1: Understand the head pointer role

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

    When the first node is deleted, the head pointer must move to the next node, which becomes the new first node.
  3. Final Answer:

    It moves to the second node in the list -> Option C
  4. Quick Check:

    Head points to new first node after deletion [OK]
Hint: Head always moves to next node after deleting first [OK]
Common Mistakes:
  • Assuming head becomes NULL even if list has more nodes
  • Not updating head pointer after deletion
  • Pointing head to last node mistakenly
2. Which of the following is the correct syntax to delete the first node in a doubly linked list in C?
easy
A. temp = head; head = head->next; free(temp); head->prev = NULL;
B. free(head); head = head->next; head->prev = NULL;
C. head = head->next; free(head->prev); head->prev = NULL;
D. head->prev = NULL; free(head); head = head->next;

Solution

  1. Step 1: Store current head in temp

    We save the current first 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 second node, free the old first node, and set new head's prev to NULL.
  3. Final Answer:

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

    Proper order: save, move, free, update pointers [OK]
Hint: Save old head before freeing, then update pointers [OK]
Common Mistakes:
  • Freeing head before moving pointer
  • Not setting new head's prev to NULL
  • Accessing freed memory after free
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> NULL, what will be the list after deleting the first node?
medium
A. 20 <-> 30 <-> NULL
B. 10 <-> 30 <-> NULL
C. 30 <-> NULL
D. NULL

Solution

  1. Step 1: Identify the first node to delete

    The first node has value 10, which will be removed.
  2. Step 2: Update head to second node and adjust pointers

    Head moves to node with value 20, and its prev pointer is set to NULL.
  3. Final Answer:

    20 <-> 30 <-> NULL -> Option A
  4. Quick Check:

    First node removed, list starts at 20 [OK]
Hint: After deletion, list starts from second node [OK]
Common Mistakes:
  • Leaving old head in list
  • Not updating prev pointer of new head
  • Deleting wrong node
4. What is wrong with this code snippet for deleting the first node of a doubly linked list?
void deleteBegin() {
  if (head == NULL) return;
  free(head);
  head = head->next;
  if (head != NULL) head->prev = NULL;
}
medium
A. It does not free the node
B. It does not check if list is empty
C. It forgets to set head->prev to NULL
D. It frees head before moving pointer, causing invalid access

Solution

  1. Step 1: Analyze order of operations

    The code frees head first, then tries to access head->next, which is invalid after free.
  2. Step 2: Correct order needed

    We must first save head->next in a temp pointer, move head, then free old node to avoid invalid memory access.
  3. Final Answer:

    It frees head before moving pointer, causing invalid access -> Option D
  4. Quick Check:

    Free after pointer update to avoid invalid access [OK]
Hint: Always move pointer before freeing old node [OK]
Common Mistakes:
  • Freeing node before updating pointers
  • Accessing freed memory
  • Not handling empty list properly
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25 <-> 35 <-> NULL. After deleting the first node twice, what will be the 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 node with value 5, head moves to 15.
  2. Step 2: Delete first node again

    Second deletion removes node with value 15, head moves to 25.
  3. Final Answer:

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

    Two deletions remove first two nodes [OK]
Hint: Each deletion moves head to next node [OK]
Common Mistakes:
  • Deleting only once instead of twice
  • Not updating head correctly after second deletion
  • Confusing node values after deletion
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