Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Insert at Specific Position in Doubly Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after inserting at position 2 in a doubly linked list
What is the printed state of the doubly linked list after inserting the value 15 at position 2?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

class DoublyLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def insert_at_position(self, data, position):
        new_node = Node(data)
        if position == 1:
            new_node.next = self.head
            if self.head:
                self.head.prev = new_node
            self.head = new_node
            return
        temp = self.head
        for _ in range(position - 2):
            if temp is None:
                return
            temp = temp.next
        if temp is None:
            return
        new_node.next = temp.next
        if temp.next:
            temp.next.prev = new_node
        temp.next = new_node
        new_node.prev = temp

    def print_list(self):
        temp = self.head
        result = []
        while temp:
            result.append(temp.data)
            temp = temp.next
        print(' -> '.join(map(str, result)) + ' -> null')

dll = DoublyLinkedList()
dll.append(10)
dll.append(20)
dll.append(30)
dll.insert_at_position(15, 2)
dll.print_list()
A10 -> 20 -> 15 -> 30 -> null
B15 -> 10 -> 20 -> 30 -> null
C10 -> 15 -> 20 -> 30 -> null
D10 -> 20 -> 30 -> 15 -> null
Attempts:
2 left
💡 Hint
Remember that position 1 means inserting at the head, so position 2 means after the first node.
❓ Predict Output
intermediate
2:00remaining
Output after inserting at head in doubly linked list
What is the printed state of the doubly linked list after inserting the value 5 at position 1?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

class DoublyLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def insert_at_position(self, data, position):
        new_node = Node(data)
        if position == 1:
            new_node.next = self.head
            if self.head:
                self.head.prev = new_node
            self.head = new_node
            return
        temp = self.head
        for _ in range(position - 2):
            if temp is None:
                return
            temp = temp.next
        if temp is None:
            return
        new_node.next = temp.next
        if temp.next:
            temp.next.prev = new_node
        temp.next = new_node
        new_node.prev = temp

    def print_list(self):
        temp = self.head
        result = []
        while temp:
            result.append(temp.data)
            temp = temp.next
        print(' -> '.join(map(str, result)) + ' -> null')

dll = DoublyLinkedList()
dll.append(10)
dll.append(20)
dll.append(30)
dll.insert_at_position(5, 1)
dll.print_list()
A5 -> 10 -> 20 -> 30 -> null
B10 -> 5 -> 20 -> 30 -> null
C10 -> 20 -> 30 -> 5 -> null
D10 -> 20 -> 5 -> 30 -> null
Attempts:
2 left
💡 Hint
Position 1 means inserting at the very start of the list.
🔧 Debug
advanced
2:00remaining
Identify the error in insert_at_position method
What error will occur when trying to insert at position 5 in a list of length 3 using this code?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

class DoublyLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def insert_at_position(self, data, position):
        new_node = Node(data)
        if position == 1:
            new_node.next = self.head
            if self.head:
                self.head.prev = new_node
            self.head = new_node
            return
        temp = self.head
        for _ in range(position - 2):
            temp = temp.next
        new_node.next = temp.next
        if temp.next:
            temp.next.prev = new_node
        temp.next = new_node
        new_node.prev = temp

dll = DoublyLinkedList()
dll.append(10)
dll.append(20)
dll.append(30)
dll.insert_at_position(40, 5)
ATypeError: unsupported operand type(s)
BIndexError: list index out of range
CNo error, inserts at the end
DAttributeError: 'NoneType' object has no attribute 'next'
Attempts:
2 left
💡 Hint
Check what happens when temp becomes None in the for loop.
🧠 Conceptual
advanced
1:30remaining
Why update prev pointer when inserting in doubly linked list?
Why is it necessary to update the prev pointer of the node after the inserted node when inserting at a specific position in a doubly linked list?
ATo free memory occupied by the previous node
BTo maintain the backward link so traversal in reverse direction works correctly
CTo avoid memory leaks in the list
DTo speed up the insertion operation
Attempts:
2 left
💡 Hint
Think about how doubly linked lists allow moving backward.
❓ Predict Output
expert
3:00remaining
Output after multiple insertions at various positions
What is the printed state of the doubly linked list after these operations? 1. Append 10 2. Append 20 3. Insert 15 at position 2 4. Insert 5 at position 1 5. Insert 25 at position 5
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

class DoublyLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def insert_at_position(self, data, position):
        new_node = Node(data)
        if position == 1:
            new_node.next = self.head
            if self.head:
                self.head.prev = new_node
            self.head = new_node
            return
        temp = self.head
        for _ in range(position - 2):
            if temp is None:
                return
            temp = temp.next
        if temp is None:
            return
        new_node.next = temp.next
        if temp.next:
            temp.next.prev = new_node
        temp.next = new_node
        new_node.prev = temp

    def print_list(self):
        temp = self.head
        result = []
        while temp:
            result.append(temp.data)
            temp = temp.next
        print(' -> '.join(map(str, result)) + ' -> null')

dll = DoublyLinkedList()
dll.append(10)
dll.append(20)
dll.insert_at_position(15, 2)
dll.insert_at_position(5, 1)
dll.insert_at_position(25, 5)
dll.print_list()
A5 -> 10 -> 15 -> 20 -> 25 -> null
B5 -> 10 -> 15 -> 25 -> 20 -> null
C10 -> 5 -> 15 -> 20 -> 25 -> null
D5 -> 15 -> 10 -> 20 -> 25 -> null
Attempts:
2 left
💡 Hint
Track each insertion carefully and remember positions start at 1.

Practice

(1/5)
1. What is the main purpose of inserting a node at a specific position in a doubly linked list?
easy
A. To add a new node exactly where you want in the list
B. To delete a node from the list
C. To reverse the entire list
D. To find the length of the list

Solution

  1. 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.
  2. Step 2: Differentiate from other operations

    Deleting removes nodes, reversing changes order, and length counts nodes, none insert at a position.
  3. Final Answer:

    To add a new node exactly where you want in the list -> Option A
  4. Quick Check:

    Insert at position = Add node at exact place [OK]
Hint: Insertion means adding, not deleting or reversing [OK]
Common Mistakes:
  • Confusing insertion with deletion
  • Thinking insertion changes list order
  • Mixing insertion with counting nodes
2. Which of the following is the correct way to update the previous pointer of the new node when inserting at position 3 in a doubly linked list?
easy
A. new_node.prev = null
B. new_node.prev = head
C. new_node.prev = head.next.next
D. new_node.prev = head.next

Solution

  1. 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).
  2. Step 2: Assign previous pointer correctly

    new_node.prev must point to the node at position 2, which is head.next.
  3. Final Answer:

    new_node.prev = head.next -> Option D
  4. Quick Check:

    Prev pointer = node before insertion [OK]
Hint: Prev points to node before insertion position [OK]
Common Mistakes:
  • Setting prev to null instead of correct node
  • Confusing prev with next pointer
  • Using head instead of node before position
3. What will be the output list after inserting a node with value 10 at position 2 in the doubly linked list 5 <-> 15 <-> 20?
medium
A. 5 <-> 15 <-> 10 <-> 20 <-> null
B. 5 <-> 10 <-> 15 <-> 20 <-> null
C. 10 <-> 5 <-> 15 <-> 20 <-> null
D. 5 <-> 15 <-> 20 <-> 10 <-> null

Solution

  1. Step 1: Understand position 2 insertion

    Position 2 means the new node goes after the first node (5) and before the second node (15).
  2. Step 2: Insert node with value 10 at position 2

    List becomes 5 <-> 10 <-> 15 <-> 20, with null at the end.
  3. Final Answer:

    5 <-> 10 <-> 15 <-> 20 <-> null -> Option B
  4. Quick Check:

    Insert at 2 shifts old 2nd node right [OK]
Hint: Insert pushes existing nodes right from position [OK]
Common Mistakes:
  • Inserting after instead of at position
  • Placing new node at wrong position
  • Confusing list order after insertion
4. Given this code snippet for insertion at position 3, what is the error?
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_node
medium
A. Loop runs one time too many causing temp to be wrong
B. new_node.prev should be temp.prev
C. new_node.next should be temp
D. Missing check if head is null before loop

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Loop runs one time too many causing temp to be wrong -> Option A
  4. Quick Check:

    Loop range should be (position - 2) [OK]
Hint: Loop iterations = position - 2 for temp at (position - 1) [OK]
Common Mistakes:
  • Loop runs one time too many
  • Confusing prev and next pointers
  • Incorrect loop count for position
5. You have a doubly linked list: 1 <-> 3 <-> 5 <-> 7 <-> null. You want to insert a node with value 4 at position 3. After insertion, what will be the correct sequence of the list?
medium
A. 4 <-> 1 <-> 3 <-> 5 <-> 7 <-> null
B. 1 <-> 4 <-> 3 <-> 5 <-> 7 <-> null
C. 1 <-> 3 <-> 4 <-> 5 <-> 7 <-> null
D. 1 <-> 3 <-> 5 <-> 7 <-> 4 <-> null

Solution

  1. 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).
  2. Step 2: Insert node with value 4 at position 3

    The list becomes 1 <-> 3 <-> 4 <-> 5 <-> 7 <-> null, maintaining order and links.
  3. Final Answer:

    1 <-> 3 <-> 4 <-> 5 <-> 7 <-> null -> Option C
  4. Quick Check:

    Insert at position 3 places node between 3 and 5 [OK]
Hint: Insert at position shifts old node at that position right [OK]
Common Mistakes:
  • Inserting before position 1 instead of 3
  • Appending at end instead of position
  • Mixing node values order after insertion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard