0
0
DSA Pythonprogramming~10 mins

Insert at Middle Specific Position in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Insert at Middle Specific Position
Start with Linked List
Check if position is valid
Yes
Traverse to node before position
Create new node
Adjust pointers to insert new node
New node inserted
End
This flow shows how to insert a new node at a specific middle position in a linked list by traversing to the correct spot and adjusting pointers.
Execution Sample
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def insert_middle(head, data, position):
    new_node = Node(data)
    if position == 0:
        new_node.next = head
        return new_node
    current = head
    for _ in range(position - 1):
        current = current.next
    new_node.next = current.next
    current.next = new_node
    return head
This code inserts a new node with given data at the specified position in a singly linked list.
Execution Table
StepActionCurrent Node DataPosition CounterPointers ChangedList State
1Create new node with data=99--None1 -> 2 -> 3 -> 4 -> null
2Check if position=2 is 0--No1 -> 2 -> 3 -> 4 -> null
3Set current to head (data=1)10None1 -> 2 -> 3 -> 4 -> null
4Traverse to position-1 (1)11None1 -> 2 -> 3 -> 4 -> null
5Set new_node.next = current.next (2)11new_node.next points to 21 -> 2 -> 3 -> 4 -> null
6Set current.next = new_node (99)11current.next points to new_node1 -> 99 -> 2 -> 3 -> 4 -> null
7Return head--None1 -> 99 -> 2 -> 3 -> 4 -> null
8End---Insertion complete
💡 Position reached and new node inserted, traversal stops
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
headNode(1)Node(1)Node(1)Node(1)Node(1)Node(1)
currentNoneNode(1)Node(1)Node(1)Node(1)Node(1)
new_nodeNoneNode(99)Node(99)Node(99)Node(99)Node(99)
position222222
Key Moments - 3 Insights
Why do we traverse to position-1 instead of position?
We stop at position-1 (Step 4) because we need to change the next pointer of the node before the insertion point to point to the new node (Step 6).
What happens if position is 0?
If position is 0 (Step 2), the new node becomes the new head by pointing its next to the current head, then returning itself as the new head.
How do pointers change to insert the new node?
First, new_node.next points to current.next (Step 5), then current.next points to new_node (Step 6), linking the new node into the list.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the data of the current node at Step 4?
A2
B1
C3
D99
💡 Hint
Check the 'Current Node Data' column at Step 4 in the execution_table.
At which step does the new node's next pointer get assigned?
AStep 3
BStep 6
CStep 5
DStep 7
💡 Hint
Look for 'new_node.next points to' in the 'Pointers Changed' column.
If position was 0, what would happen according to the code?
AThe new node becomes the new head
BThe new node is inserted at the end
CThe list remains unchanged
DAn error occurs
💡 Hint
Refer to Step 2 where position == 0 is checked.
Concept Snapshot
Insert at Middle Specific Position in Linked List:
- Traverse to node before target position (position-1).
- Create new node.
- Set new_node.next to current.next.
- Set current.next to new_node.
- If position is 0, new node becomes new head.
- Adjust pointers carefully to avoid breaking the list.
Full Transcript
This lesson shows how to insert a new node at a specific middle position in a singly linked list. We start by checking if the position is zero, which means inserting at the head. Otherwise, we traverse the list to the node just before the desired position. Then we create a new node and adjust pointers: the new node's next points to the current node's next, and the current node's next points to the new node. This links the new node into the list without losing any nodes. The execution table traces each step, showing how variables and pointers change. Key moments clarify why we stop at position-1 and how pointers are updated. The visual quiz tests understanding of these steps. This method keeps the list connected and inserts the new node exactly where needed.