0
0
DSA Pythonprogramming~20 mins

Create and Initialize Doubly Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Doubly Linked List after Insertions
What is the printed state of the doubly linked list after inserting nodes with values 10, 20, and 30 at the end?
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 print_list(self):
        current = self.head
        result = []
        while current:
            result.append(str(current.data))
            current = current.next
        print(" <-> ".join(result) + " <-> null")

dl = DoublyLinkedList()
dl.append(10)
dl.append(20)
dl.append(30)
dl.print_list()
A10 -> 20 -> 30 -> null
B30 <-> 20 <-> 10 <-> null
C10 <-> 20 <-> 30 <-> null
Dnull <-> 10 <-> 20 <-> 30
Attempts:
2 left
💡 Hint
Trace the append method and how nodes link forward and backward.
Predict Output
intermediate
2:00remaining
State of Doubly Linked List after Prepending Nodes
What is the printed state of the doubly linked list after prepending nodes with values 5, 15, and 25 in that order?
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 prepend(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_list(self):
        current = self.head
        result = []
        while current:
            result.append(str(current.data))
            current = current.next
        print(" <-> ".join(result) + " <-> null")

dl = DoublyLinkedList()
dl.prepend(5)
dl.prepend(15)
dl.prepend(25)
dl.print_list()
A25 <-> 15 <-> 5 <-> null
B5 <-> 15 <-> 25 <-> null
Cnull <-> 25 <-> 15 <-> 5
D15 <-> 25 <-> 5 <-> null
Attempts:
2 left
💡 Hint
Prepending adds nodes at the start, so the last prepended node becomes the head.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Doubly Linked List Node Removal
What error will occur when trying to remove the node with value 20 using the given remove method?
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 remove(self, data):
        current = self.head
        while current:
            if current.data == data:
                if current.prev:
                    current.prev.next = current.next
                else:
                    self.head = current.next
                if current.next:
                    current.next.prev = current.prev
                return
            current = current.next

    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.append(10)
dl.append(20)
dl.append(30)
dl.remove(20)
dl.print_list()
AValueError: data not found
BNo error, output: 10 <-> 30 <-> null
CAttributeError: 'NoneType' object has no attribute 'next'
DTypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
Attempts:
2 left
💡 Hint
Check what happens when removing the head node and how head is updated.
Predict Output
advanced
2:00remaining
Output after Reversing a Doubly Linked List
What is the printed state of the doubly linked list after reversing it?
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 reverse(self):
        current = self.head
        temp = None
        while current:
            temp = current.prev
            current.prev = current.next
            current.next = temp
            current = current.prev
        if temp:
            self.head = temp.prev

    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.append(1)
dl.append(2)
dl.append(3)
dl.append(4)
dl.reverse()
dl.print_list()
A4 <-> 3 <-> 2 <-> 1 <-> null
B1 <-> 2 <-> 3 <-> 4 <-> null
Cnull <-> 4 <-> 3 <-> 2 <-> 1
D1 -> 2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Reversing swaps next and prev pointers for each node and updates head to last node.
🧠 Conceptual
expert
2:00remaining
Memory Usage in Doubly Linked List vs Singly Linked List
Which statement correctly explains the difference in memory usage between a doubly linked list and a singly linked list?
AA singly linked list uses more memory because it requires additional pointers to keep track of the previous node.
BA doubly linked list uses less memory because it can be traversed in both directions, reducing the need for extra nodes.
CBoth doubly and singly linked lists use the same memory per node because pointers are optimized automatically by the system.
DA doubly linked list uses more memory per node because it stores two pointers (prev and next), while a singly linked list stores only one pointer (next).
Attempts:
2 left
💡 Hint
Consider how many pointers each node stores in both list types.