Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Doubly Linked List Structure and Node Design in DSA Python - Practice Problems & Challenges

Choose your learning style10 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 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")

dll = DoublyLinkedList()
dll.append(10)
dll.append(20)
dll.append(30)
dll.print_list()
A10 -> 20 -> 30 -> null
B30 -> 20 -> 10 -> null
Cnull
D10 -> 20 -> null
Attempts:
2 left
💡 Hint
Think about how nodes are added at the end and how the print_list method traverses.
❓ Predict Output
intermediate
2:00remaining
Value of Node after Deletion
What is the value of the head node after deleting the first node from the doubly linked list containing 5 -> 15 -> 25 -> null?
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 delete_first(self):
        if not self.head:
            return
        if not self.head.next:
            self.head = None
            return
        self.head = self.head.next
        self.head.prev = None

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

# Setup
list_obj = DoublyLinkedList()
list_obj.append(5)
list_obj.append(15)
list_obj.append(25)
list_obj.delete_first()
print(list_obj.head.data if list_obj.head else 'null')
A15
B5
C25
Dnull
Attempts:
2 left
💡 Hint
Deleting the first node moves the head to the next node.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Node Link Update
What error occurs when running this code that inserts a new node after the head in a doubly linked list?
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_after_head(self, data):
        if not self.head:
            return
        new_node = Node(data)
        new_node.next = self.head.next
        if self.head.next:
            self.head.next.prev = new_node
        self.head.next = new_node
        new_node.prev = self.head

# Setup
list_obj = DoublyLinkedList()
list_obj.append(1)
list_obj.insert_after_head(2)
list_obj.insert_after_head(3)
ANo error, code runs successfully
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CAttributeError: 'NoneType' object has no attribute 'prev'
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Check what happens when head.next is None and you try to access head.next.prev.
🧠 Conceptual
advanced
1:30remaining
Understanding Node Connections in Doubly Linked List
In a doubly linked list, if a node's prev pointer is not updated correctly during insertion, what is the most likely consequence?
AThe list will automatically fix the pointers during traversal.
BThe list may become corrupted causing traversal errors backwards.
CThe node will be removed automatically from the list.
DThe list will convert into a singly linked list.
Attempts:
2 left
💡 Hint
Think about how prev pointers help in moving backwards through the list.
🚀 Application
expert
3:00remaining
Resulting List After Complex Operations
Given the following operations on an empty doubly linked list, what is the final printed list? Operations: 1. Append 100 2. Append 200 3. Insert 150 after head 4. Delete first node 5. Append 250 6. Insert 175 after node with value 150 Print the list from head to tail.
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_after(self, prev_node_data, data):
        current = self.head
        while current and current.data != prev_node_data:
            current = current.next
        if not current:
            return
        new_node = Node(data)
        new_node.next = current.next
        if current.next:
            current.next.prev = new_node
        current.next = new_node
        new_node.prev = current

    def delete_first(self):
        if not self.head:
            return
        if not self.head.next:
            self.head = None
            return
        self.head = self.head.next
        self.head.prev = None

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

# Operations
list_obj = DoublyLinkedList()
list_obj.append(100)
list_obj.append(200)
list_obj.insert_after(100, 150)
list_obj.delete_first()
list_obj.append(250)
list_obj.insert_after(150, 175)
list_obj.print_list()
A100 -> 150 -> 175 -> 200 -> 250 -> null
B150 -> 200 -> 175 -> 250 -> null
C200 -> 150 -> 175 -> 250 -> null
D150 -> 175 -> 200 -> 250 -> null
Attempts:
2 left
💡 Hint
Follow each operation step-by-step and update the list accordingly.

Practice

(1/5)
1. What is the main difference between a doubly linked list node and a singly linked list node?
easy
A. A doubly linked list node does not store any data.
B. A doubly linked list node has pointers to both the previous and next nodes.
C. A doubly linked list node can only point to the next node.
D. A doubly linked list node stores data in a different format.

Solution

  1. Step 1: Understand node pointers in singly linked list

    A singly linked list node has only one pointer to the next node.
  2. Step 2: Understand node pointers in doubly linked list

    A doubly linked list node has two pointers: one to the previous node and one to the next node.
  3. Final Answer:

    A doubly linked list node has pointers to both the previous and next nodes. -> Option B
  4. Quick Check:

    Doubly linked list nodes have two pointers = D [OK]
Hint: Remember: doubly means two pointers, singly means one [OK]
Common Mistakes:
  • Thinking doubly linked list nodes store data differently
  • Confusing pointer count between singly and doubly linked lists
  • Assuming doubly linked list nodes have only one pointer
2. Which of the following is the correct way to define a doubly linked list node class in Python?
easy
A. class Node: def __init__(self, data): self.data = data self.next = None
B. class Node: def __init__(self, data): self.next = None self.prev = None
C. class Node: def __init__(self, data): self.data = data self.prev = None
D. class Node: def __init__(self, data): self.data = data self.prev = None self.next = None

Solution

  1. Step 1: Check for data storage and pointers

    A doubly linked list node must store data and have both prev and next pointers.
  2. Step 2: Verify class definitions

    class Node: def __init__(self, data): self.data = data self.prev = None self.next = None correctly defines data, prev, and next attributes in __init__.
  3. Final Answer:

    class Node:\n def __init__(self, data):\n self.data = data\n self.prev = None\n self.next = None -> Option D
  4. Quick Check:

    Node class with data, prev, next = B [OK]
Hint: Doubly linked nodes need both prev and next pointers [OK]
Common Mistakes:
  • Omitting the prev pointer
  • Not initializing data attribute
  • Defining only next pointer like singly linked list
3. What will be the output of the following code snippet?
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

head = Node(1)
second = Node(2)
third = Node(3)

head.next = second
second.prev = head
second.next = third
third.prev = second

current = third
result = []
while current:
    result.append(current.data)
    current = current.prev
print(result)
medium
A. [3, 2, 1]
B. [1, 2, 3]
C. [1, 3, 2]
D. [3, 1, 2]

Solution

  1. Step 1: Understand the linked list connections

    Nodes are linked as 1 <-> 2 <-> 3 with prev and next pointers set correctly.
  2. Step 2: Trace the while loop starting from third node

    Starting at node with data 3, append 3, move to prev (2), append 2, move to prev (1), append 1, then prev is None, loop ends.
  3. Final Answer:

    [3, 2, 1] -> Option A
  4. Quick Check:

    Traverse backward from third node = [3, 2, 1] [OK]
Hint: Backward traversal collects nodes in reverse order [OK]
Common Mistakes:
  • Confusing next and prev pointers during traversal
  • Starting traversal from head instead of third
  • Appending data in wrong order
4. Identify the error in the following doubly linked list node insertion code and select the correct fix.
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

head = Node(1)
new_node = Node(2)

# Insert new_node after head
new_node.next = head.next
head.next.prev = new_node
head.next = new_node
new_node.prev = head
medium
A. No error; code inserts node correctly.
B. new_node.prev should be assigned before head.next.
C. head.next.prev causes AttributeError; fix by checking if head.next is not None before accessing prev.
D. new_node.next should be assigned after head.next.

Solution

  1. Step 1: Analyze head.next before insertion

    Initially, head.next is None, so head.next.prev causes AttributeError.
  2. Step 2: Fix by checking if head.next exists before accessing prev

    Adding a condition to check if head.next is not None before setting head.next.prev avoids error.
  3. Final Answer:

    head.next.prev causes AttributeError; fix by checking if head.next is not None before accessing prev. -> Option C
  4. Quick Check:

    Check for None before accessing prev pointer [OK]
Hint: Always check if next node exists before accessing its prev [OK]
Common Mistakes:
  • Ignoring None check causing runtime error
  • Assigning pointers in wrong order
  • Assuming head.next is always valid
5. You want to insert a new node with value 4 between nodes with values 2 and 3 in a doubly linked list. Given the nodes are connected as 1 <-> 2 <-> 3, which sequence of pointer updates correctly inserts the new node?
hard
A. new_node.prev = node2; new_node.next = node3; node2.next = new_node; node3.prev = new_node
B. new_node.next = node2; new_node.prev = node3; node3.next = new_node; node2.prev = new_node
C. node2.next = new_node; new_node.next = node3; node3.next = new_node; new_node.prev = node2
D. node3.prev = new_node; new_node.prev = node2; node2.next = new_node; new_node.next = node2

Solution

  1. Step 1: Set new_node pointers to neighbors

    new_node.prev should point to node2 and new_node.next to node3 to link correctly.
  2. Step 2: Update neighbors to point to new_node

    node2.next must point to new_node and node3.prev must point to new_node to complete insertion.
  3. Final Answer:

    new_node.prev = node2; new_node.next = node3; node2.next = new_node; node3.prev = new_node -> Option A
  4. Quick Check:

    Set new_node pointers then update neighbors [OK]
Hint: Link new node to neighbors, then neighbors back to new node [OK]
Common Mistakes:
  • Swapping prev and next pointers
  • Updating neighbors before setting new_node pointers
  • Forgetting to update one neighbor pointer
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