0
0
DSA Pythonprogramming~20 mins

Insert at Beginning of Doubly Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output after inserting nodes at the beginning
What is the printed state of the doubly linked list after inserting 10, then 20, then 30 at the beginning?
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 insert_at_beginning(self, data):
        new_node = Node(data)
        new_node.next = self.head
        if self.head is not None:
            self.head.prev = new_node
        self.head = new_node

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

dl = DoublyLinkedList()
dl.insert_at_beginning(10)
dl.insert_at_beginning(20)
dl.insert_at_beginning(30)
dl.print_list()
A30 -> 20 -> 10 -> null
B10 -> 20 -> 30 -> null
C20 -> 30 -> 10 -> null
D30 -> 10 -> 20 -> null
Attempts:
2 left
💡 Hint
Remember that inserting at the beginning means the newest node becomes the head.
🧠 Conceptual
intermediate
1:30remaining
Understanding pointer updates in insertion at beginning
When inserting a new node at the beginning of a doubly linked list, which pointers must be updated to maintain list integrity?
ANew node's next and prev both point to old head; head remains unchanged
BNew node's next points to old head; old head's prev points to new node; head updated to new node
CNew node's prev points to old head; old head's next points to new node; head updated to new node
DOnly head pointer is updated to new node; no other pointers change
Attempts:
2 left
💡 Hint
Think about how the new node connects forward and backward to the existing list.
🔧 Debug
advanced
2:00remaining
Identify the error in this insertion code
What error will this code cause when inserting at the beginning of a doubly linked list?
DSA Python
def insert_at_beginning(self, data):
    new_node = Node(data)
    new_node.prev = self.head
    self.head = new_node
    if new_node.next is not None:
        new_node.next.prev = new_node
AAttributeError because new_node.next is None and accessed without assignment
BNo error, code works correctly
CLogical error: new_node.prev should be None, not self.head
DTypeError because new_node.prev is assigned a Node object instead of None
Attempts:
2 left
💡 Hint
Check what prev pointer of the new head node should be.
Predict Output
advanced
2:00remaining
Output after multiple insertions and one traversal
What is the output of the following 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 insert_at_beginning(self, data):
        new_node = Node(data)
        new_node.next = self.head
        if self.head:
            self.head.prev = new_node
        self.head = new_node

    def print_reverse(self):
        current = self.head
        while current and current.next:
            current = current.next
        result = []
        while current:
            result.append(str(current.data))
            current = current.prev
        print(' <- '.join(result) + ' <- null')

dl = DoublyLinkedList()
dl.insert_at_beginning(1)
dl.insert_at_beginning(2)
dl.insert_at_beginning(3)
dl.print_reverse()
A1 <- 2 <- 3 <- null
B3 <- 2 <- 1 <- null
Cnull
D1 -> 2 -> 3 -> null
Attempts:
2 left
💡 Hint
The print_reverse method prints from tail to head.
🧠 Conceptual
expert
1:30remaining
Why is updating the previous pointer important in doubly linked list insertion?
What problem arises if you insert a node at the beginning of a doubly linked list but forget to update the old head's prev pointer to the new node?
ANo problem; the list will work fine without updating prev pointer
BForward traversal will skip the new node, losing data
CThe list will become singly linked automatically
DBackward traversal from the old head will be broken, causing incorrect or incomplete reverse iteration
Attempts:
2 left
💡 Hint
Think about how prev pointers help in moving backward through the list.