0
0
DSA Pythonprogramming~10 mins

Delete Node at End in DSA Python - 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
Set second last node's next to null
Delete last node
End
Start from the head, check if list is empty, then move to the second last node and remove the last node by updating pointers.
Execution Sample
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def delete_end(head):
    if not head:
        return None
    if not head.next:
        return None
    temp = head
    while temp.next.next:
        temp = temp.next
    temp.next = None
    return head
Deletes the last node from a singly linked list and returns the updated head.
Execution Table
StepActionCurrent Node DataNext Node DataPointer UpdateList State
1Check if head is None12No1 -> 2 -> 3 -> None
2Check if head.next is None12No1 -> 2 -> 3 -> None
3Set temp = head12No1 -> 2 -> 3 -> None
4Check temp.next.next exists12No1 -> 2 -> 3 -> None
5Move temp to temp.next23No1 -> 2 -> 3 -> None
6Check temp.next.next exists23No1 -> 2 -> 3 -> None
7temp.next.next is None, stop loop23No1 -> 2 -> 3 -> None
8Set temp.next = None (delete last node)2NoneYes1 -> 2 -> None
9Return head12No1 -> 2 -> None
💡 Last node deleted by setting second last node's next to None
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 8Final
headNode(1)Node(1)Node(1)Node(1)Node(1)
tempNoneNode(1)Node(2)Node(2)Node(2)
Key Moments - 3 Insights
Why do we check if head.next is None before traversing?
Because if head.next is None, it means there is only one node. We delete it by returning None. This is shown in execution_table row 2.
Why do we use temp.next.next in the while loop condition?
To stop at the second last node, so we can remove the last node by setting temp.next to None. This is shown in execution_table rows 4-7.
What happens if the list is empty at the start?
The function returns None immediately, meaning no deletion happens. This is shown in execution_table row 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of temp after step 5?
ANode with data 2
BNode with data 1
CNode with data 3
DNone
💡 Hint
Check the 'Current Node Data' column at step 5 in execution_table
At which step does the last node get deleted?
AStep 4
BStep 7
CStep 8
DStep 9
💡 Hint
Look for the step where 'Pointer Update' is 'Yes' in execution_table
If the list had only one node, what would the function return?
AThe same single node
BNone
CA new node
DAn error
💡 Hint
Refer to execution_table row 2 where head.next is None
Concept Snapshot
Delete Node at End in singly linked list:
- Check if list empty or single node
- Traverse to second last node
- Set second last node's next to None
- Last node is removed
- Return updated head
Full Transcript
This visual execution shows how to delete the last node in a singly linked list. We start by checking if the list is empty or has only one node. If empty, we return None. If only one node, we delete it by returning None. Otherwise, we traverse the list to find the second last node by checking temp.next.next. Once found, we set temp.next to None, removing the last node. The updated list is then returned. The execution table traces each step, showing variable values and list state. Key moments clarify why we check conditions and how traversal works. The quiz tests understanding of variable states and deletion step.