0
0
DSA Pythonprogramming~10 mins

Insert at Beginning of Circular Linked List in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Insert at Beginning of Circular Linked List
Create new node
↓
Is list empty?
Yes→new_node.next = new_node
|No↓
Traverse to last node
↓
last_node.next = new_node
↓
new_node.next = head
↓
Update head = new_node
↓
Done
Start by creating a new node. If the list is empty, point new node to itself. Otherwise, find the last node, link it to new node, link new node to old head, then update head.
Execution Sample
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def insert_at_beginning(head, data):
    new_node = Node(data)
    if head is None:
        new_node.next = new_node
        head = new_node
        return head
    current = head
    while current.next != head:
        current = current.next
    current.next = new_node
    new_node.next = head
    head = new_node
    return head
This code creates a new node and inserts it at the beginning of a circular linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with empty listāˆ…head = NoneList is empty
2Create new node with data=10Node(10)new_node.next = Nonenew_node: [10 | next: āˆ…]
3Check if list is emptyNode(10)head is None → TrueNo links yet
4Point new_node.next to itself (single node circular)Node(10)new_node.next = new_nodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ next:──┼──→ (points to self) ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head → new_node
5Update head to new_nodeNode(10)head = new_nodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ next:──┼──→ (points to self) ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head → new_node
6Insert new node with data=20 at beginningNode(10) → Node(20)Create new_node(20)new_node: [20 | next: āˆ…]
7Traverse to last node (node with data=10)Node(10) → Node(20)current = head (10) current.next != head? No (points to 10 itself)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ next:──┼──→ │ next: āˆ…ā”‚ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
8Set last_node.next to new_nodeNode(10) → Node(20)last_node(10).next = new_node(20)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ next:──┼──→ │ next: āˆ…ā”‚ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
9Set new_node.next to old headNode(10) → Node(20)new_node(20).next = head(10)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ next:──┼──→ │ next:──┼──→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head new_node
10Update head to new_nodeNode(20) → Node(10)head = new_node(20)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:10│ │ next:──┼──→ │ next:──┼──→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head last_node (new head)
11Insert new node with data=30 at beginningNode(20) → Node(10) → Node(30)Create new_node(30)new_node: [30 | next: āˆ…]
12Traverse to last node (node with data=10)Node(20) → Node(10) → Node(30)current = head(20) current.next != head? Yes (10) Move to 10 current.next != head? No (20)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:10│ │ data:30│ │ next:──┼──→ │ next:──┼──→ │ next: āˆ…ā”‚ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
13Set last_node.next to new_nodeNode(20) → Node(10) → Node(30)last_node(10).next = new_node(30)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:10│ │ data:30│ │ next:──┼──→ │ next:──┼──→ │ next: āˆ…ā”‚ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
14Set new_node.next to old headNode(20) → Node(10) → Node(30)new_node(30).next = head(20)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:10│ │ data:30│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head last_node
15Update head to new_nodeNode(30) → Node(20) → Node(10)head = new_node(30)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:30│ │ data:20│ │ data:10│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head second last_node
16StopNode(30) → Node(20) → Node(10)Insertion completeCircular linked list updated with new head
šŸ’” Insertion stops after updating head to new node and linking last node to new head.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5After Step 6After Step 9After Step 10After Step 11After Step 14After Step 15Final
headNoneNoneNoneNode(10)Node(10)Node(10)Node(20)Node(20)Node(20)Node(30)Node(30)
new_nodeNoneNode(10)Node(10)Node(10)Node(20)Node(20)Node(20)Node(30)Node(30)Node(30)Node(30)
last_nodeNoneNoneNoneNoneNode(10)Node(10)Node(10)Node(10)Node(10)Node(10)Node(10)
currentNoneNoneNoneNoneNode(10)Node(10)Node(20)Node(20)Node(20)Node(20)Node(20)
Key Moments - 3 Insights
Why do we need to traverse to the last node before inserting at the beginning?
Because in a circular linked list, the last node points back to the head. To insert a new node at the beginning, we must update the last node's next pointer to the new node. This is shown in steps 7-9 in the execution_table.
What happens if the list is empty when inserting the first node?
If the list is empty (head is None), the new node points to itself, making a single-node circular list. This is shown in steps 3-5 in the execution_table.
Why do we update the head pointer after insertion?
Because the new node becomes the first node in the list, so head must point to it. This update is shown in steps 5, 10, and 15.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what does new_node.next point to?
AIt points to None
BIt points to itself (new_node)
CIt points to the old head
DIt points to the last node
šŸ’” Hint
Check the Pointer Changes and Visual State columns at step 4.
At which step does the head pointer update to the new node for the second insertion?
AStep 15
BStep 14
CStep 10
DStep 9
šŸ’” Hint
Look at the Pointer Changes column for head updates after inserting data=20.
If the list was empty, what would be the visual state after insertion?
ATwo nodes linked circularly
BA single node pointing to None
CA single node pointing to itself
DNo nodes
šŸ’” Hint
Refer to step 4 and 5 visual states for empty list insertion.
Concept Snapshot
Insert at Beginning of Circular Linked List:
- Create new node
- If empty, point new_node.next to itself
- Else, find last node
- last_node.next = new_node
- new_node.next = old head
- Update head = new_node
- List remains circular
Full Transcript
To insert a node at the beginning of a circular linked list, first create the new node. If the list is empty, make the new node point to itself and update head to it. If not empty, traverse the list to find the last node, update last node's next pointer to the new node, then set new node's next to the old head. Finally, update head to the new node. This keeps the list circular and updates the start correctly.