Bird
0
0
DSA Cprogramming~10 mins

Delete Node at End in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Delete Node at End
Start at head
Check if list empty?
YesNothing to delete, EXIT
No
Traverse to second last node
Remove last node
Update second last node's next to NULL
Free last node memory
End
Start from the head, check if list is empty, then move to the second last node, remove the last node by updating pointers and freeing memory.
Execution Sample
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

void deleteAtEnd(Node** head) {
    if (*head == NULL) return;
    if ((*head)->next == NULL) {
        free(*head);
        *head = NULL;
        return;
    }
    Node* temp = *head;
    while (temp->next->next != NULL) temp = temp->next;
    free(temp->next);
    temp->next = NULL;
}
Deletes the last node of a singly linked list by traversing to the second last node and freeing the last node.
Execution Table
StepActionPointer PositionsConditionResult
1Check if head is NULLhead -> Node1head == NULL? NoContinue
2Check if head->next is NULLhead -> Node1head->next == NULL? NoContinue
3Set temp = headtemp -> Node1-temp points to Node1
4Check temp->next->next != NULLtemp -> Node1Node2->next != NULL? YesMove temp to next
5Move temp to nexttemp -> Node2-temp points to Node2
6Check temp->next->next != NULLtemp -> Node2Node3->next != NULL? NoStop loop
7Free temp->next (Node3)temp -> Node2-Node3 freed
8Set temp->next = NULLtemp -> Node2-Node2->next = NULL
9Endhead -> Node1-Last node deleted, list ends at Node2
💡 Loop ends when temp->next->next is NULL, meaning temp is second last node.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 6Final
headNode1 -> Node2 -> Node3 -> NULLNode1 -> Node2 -> Node3 -> NULLNode1 -> Node2 -> Node3 -> NULLNode1 -> Node2 -> Node3 -> NULLNode1 -> Node2 -> NULL
tempNULLNode1Node2Node2Node2
Key Moments - 3 Insights
Why do we check temp->next->next != NULL instead of temp->next != NULL in the loop?
Because we want temp to stop at the second last node, so that temp->next is the last node to delete. This is shown in execution_table rows 4-6.
What happens if the list has only one node?
The code frees the head node and sets head to NULL immediately (rows 1-2), avoiding traversal.
Why do we set temp->next = NULL after freeing the last node?
To update the second last node's next pointer to NULL, marking the new end of the list (row 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what node does temp point to after Step 5?
ANode1
BNode2
CNode3
DNULL
💡 Hint
Check the 'Pointer Positions' column at Step 5 in execution_table.
At which step does the loop stop traversing the list?
AStep 6
BStep 5
CStep 4
DStep 7
💡 Hint
Look for the step where condition temp->next->next != NULL becomes false in execution_table.
If the list had only one node, which step would free that node?
AStep 1
BStep 7
CStep 2
DStep 8
💡 Hint
Refer to the early check for single node list in execution_table rows 1-2.
Concept Snapshot
Delete Node at End in singly linked list:
- If list empty, do nothing.
- If only one node, free it and set head NULL.
- Else, traverse to second last node.
- Free last node and set second last's next to NULL.
- Updates list end safely.
Full Transcript
This visual execution traces deleting the last node in a singly linked list. It starts by checking if the list is empty or has only one node. If one node, it frees it and sets head to NULL. Otherwise, it traverses to the second last node by checking temp->next->next != NULL. Once at second last node, it frees the last node and sets second last's next pointer to NULL, marking the new end of the list. Variables head and temp change as shown. Key moments include why we check temp->next->next, handling single node list, and updating pointers after deletion. The quiz tests understanding of pointer positions and loop stopping conditions.