Insert at Specific Position in Doubly Linked List in DSA Python - Time & Space Complexity
When inserting a new item at a certain place in a doubly linked list, we want to know how the time needed changes as the list grows.
We ask: How does the number of steps grow when the list gets longer?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
def insert_at_position(head, data, pos):
new_node = Node(data)
if pos == 0:
new_node.next = head
if head:
head.prev = new_node
return new_node
current = head
for _ in range(pos - 1):
if current is None:
return head
current = current.next
if current is None:
return head
new_node.next = current.next
if current.next:
current.next.prev = new_node
current.next = new_node
new_node.prev = current
return head
This code inserts a new node at a given position in a doubly linked list by moving through the list to find the right spot.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that moves through the list nodes to reach the position before insertion.
- How many times: Up to pos - 1 times, which depends on the position where we insert.
As the list gets longer, the steps to reach the insertion point grow roughly in direct proportion to the position.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 9 steps to reach position |
| 100 | Up to 99 steps to reach position |
| 1000 | Up to 999 steps to reach position |
Pattern observation: The time grows linearly with the position where we insert, which can be up to the size of the list.
Time Complexity: O(n)
This means the time to insert grows in a straight line with the list size or the position where we insert.
[X] Wrong: "Inserting at any position in a doubly linked list always takes constant time because we have links both ways."
[OK] Correct: Even with links both ways, we must first find the position by moving through nodes, which takes time proportional to the position.
Understanding how insertion time depends on position helps you explain and reason about linked list operations clearly, a skill valued in many coding discussions.
"What if we kept a pointer to the tail and inserted near the end? How would the time complexity change?"
Practice
Solution
Step 1: Understand insertion in doubly linked list
Insertion at a specific position means adding a new node exactly at that place, shifting others if needed.Step 2: Differentiate from other operations
Deleting removes nodes, reversing changes order, and length counts nodes, none insert at a position.Final Answer:
To add a new node exactly where you want in the list -> Option AQuick Check:
Insert at position = Add node at exact place [OK]
- Confusing insertion with deletion
- Thinking insertion changes list order
- Mixing insertion with counting nodes
Solution
Step 1: Identify the node before position 3
Position 3 means the new node comes after the second node, so prev points to node at position 2 (head.next).Step 2: Assign previous pointer correctly
new_node.prev must point to the node at position 2, which is head.next.Final Answer:
new_node.prev = head.next -> Option DQuick Check:
Prev pointer = node before insertion [OK]
- Setting prev to null instead of correct node
- Confusing prev with next pointer
- Using head instead of node before position
Solution
Step 1: Understand position 2 insertion
Position 2 means the new node goes after the first node (5) and before the second node (15).Step 2: Insert node with value 10 at position 2
List becomes 5 <-> 10 <-> 15 <-> 20, with null at the end.Final Answer:
5 <-> 10 <-> 15 <-> 20 <-> null -> Option BQuick Check:
Insert at 2 shifts old 2nd node right [OK]
- Inserting after instead of at position
- Placing new node at wrong position
- Confusing list order after insertion
temp = head
for _ in range(2):
temp = temp.next
new_node.next = temp.next
new_node.prev = temp
if temp.next:
temp.next.prev = new_node
temp.next = new_nodeSolution
Step 1: Analyze loop and temp assignment
The loop with range(2) moves temp from head (position 1) to position 2, then to position 3.Step 2: Identify the position error
For insertion at position 3 (new node becomes position 3), temp must be at position 2 to insert after it; range(2) overshoots to position 3, so loop runs one time too many.Final Answer:
Loop runs one time too many causing temp to be wrong -> Option AQuick Check:
Loop range should be (position - 2) [OK]
- Loop runs one time too many
- Confusing prev and next pointers
- Incorrect loop count for position
Solution
Step 1: Identify position 3 in the list
Position 3 means the new node goes after the second node (3) and before the third node (5).Step 2: Insert node with value 4 at position 3
The list becomes 1 <-> 3 <-> 4 <-> 5 <-> 7 <-> null, maintaining order and links.Final Answer:
1 <-> 3 <-> 4 <-> 5 <-> 7 <-> null -> Option CQuick Check:
Insert at position 3 places node between 3 and 5 [OK]
- Inserting before position 1 instead of 3
- Appending at end instead of position
- Mixing node values order after insertion
Ready to prove it?
Pass the test and this pattern moves unknown
