0
0
DSA Pythonprogramming~20 mins

Insert at Middle Specific Position in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Insert at Middle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output after inserting at position 3?
Consider a singly linked list with nodes containing values 10 -> 20 -> 30 -> 40 -> null. We insert a new node with value 25 at position 3 (1-based index). What is the resulting list?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def insert_at_position(head, data, pos):
    new_node = Node(data)
    if pos == 1:
        new_node.next = head
        return new_node
    current = head
    count = 1
    while current and count < pos - 1:
        current = current.next
        count += 1
    if not current:
        return head
    new_node.next = current.next
    current.next = new_node
    return head

def print_list(head):
    result = []
    current = head
    while current:
        result.append(current.data)
        current = current.next
    return result

head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
head.next.next.next = Node(40)

head = insert_at_position(head, 25, 3)
print(print_list(head))
A[25, 10, 20, 30, 40]
B[10, 20, 25, 30, 40]
C[10, 25, 20, 30, 40]
D[10, 20, 30, 40, 25]
Attempts:
2 left
💡 Hint
Remember that position 3 means the new node should be the third node in the list.
Predict Output
intermediate
2:00remaining
What is the output after inserting at position 1?
Given a linked list 5 -> 15 -> 25 -> null, insert a node with value 1 at position 1. What is the resulting list?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def insert_at_position(head, data, pos):
    new_node = Node(data)
    if pos == 1:
        new_node.next = head
        return new_node
    current = head
    count = 1
    while current and count < pos - 1:
        current = current.next
        count += 1
    if not current:
        return head
    new_node.next = current.next
    current.next = new_node
    return head

def print_list(head):
    result = []
    current = head
    while current:
        result.append(current.data)
        current = current.next
    return result

head = Node(5)
head.next = Node(15)
head.next.next = Node(25)

head = insert_at_position(head, 1, 1)
print(print_list(head))
A[1, 5, 15, 25]
B[5, 1, 15, 25]
C[5, 15, 25, 1]
D[1, 15, 25]
Attempts:
2 left
💡 Hint
Inserting at position 1 means the new node becomes the new head.
🔧 Debug
advanced
2:30remaining
Why does this insertion code fail to insert at position 4?
The code below tries to insert a node with value 50 at position 4 in the list 10 -> 20 -> 30 -> null, but the list remains unchanged. What is the bug?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def insert_at_position(head, data, pos):
    new_node = Node(data)
    current = head
    count = 1
    while current and count < pos:
        current = current.next
        count += 1
    if not current:
        return head
    new_node.next = current.next
    current.next = new_node
    return head

head = Node(10)
head.next = Node(20)
head.next.next = Node(30)

head = insert_at_position(head, 50, 4)

# Print list
result = []
cur = head
while cur:
    result.append(cur.data)
    cur = cur.next
print(result)
AThe loop condition should be count < pos - 1 to stop at the node before insertion point.
BThe position variable is off by one; it should start counting from zero.
CThe function should check if head is None before insertion.
DThe new node's next should be set to None explicitly before insertion.
Attempts:
2 left
💡 Hint
Think about where the pointer 'current' stops before insertion.
Predict Output
advanced
2:00remaining
What is the output after inserting at position beyond list length?
Given a linked list 1 -> 2 -> 3 -> null, what is the output after trying to insert 99 at position 5?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def insert_at_position(head, data, pos):
    new_node = Node(data)
    if pos == 1:
        new_node.next = head
        return new_node
    current = head
    count = 1
    while current and count < pos - 1:
        current = current.next
        count += 1
    if not current:
        return head
    new_node.next = current.next
    current.next = new_node
    return head

def print_list(head):
    result = []
    current = head
    while current:
        result.append(current.data)
        current = current.next
    return result

head = Node(1)
head.next = Node(2)
head.next.next = Node(3)

head = insert_at_position(head, 99, 5)
print(print_list(head))
A[99, 1, 2, 3]
B[1, 2, 3, 99]
C[1, 2, 3]
DIndexError
Attempts:
2 left
💡 Hint
If the position is beyond the list length, the list remains unchanged.
🧠 Conceptual
expert
1:30remaining
What is the time complexity of inserting at a specific middle position in a singly linked list?
Assuming a singly linked list with n nodes, what is the time complexity to insert a new node at position k (1 <= k <= n+1)?
AO(log n) because linked lists use binary search to find positions.
BO(1) because insertion is always constant time in linked lists.
CO(n) because we always traverse the entire list regardless of position.
DO(k) because we must traverse to the (k-1)th node before insertion.
Attempts:
2 left
💡 Hint
Think about how many nodes you must visit before inserting.