Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Delete from Beginning of Doubly Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Deletion 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 deleting the first node?
Consider a doubly linked list with nodes 10 <-> 20 <-> 30. After deleting the first node, what is the printed 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 delete_from_beginning(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(current.data)
            current = current.next
        print(result)

# Setup
dll = DoublyLinkedList()
dll.append(10)
dll.append(20)
dll.append(30)

# Delete first node
dll.delete_from_beginning()

# Print list
dll.print_list()
A[10, 20, 30]
B[20, 30]
C[30]
D[]
Attempts:
2 left
💡 Hint
Deleting from beginning removes the head node and updates the head pointer.
❓ Predict Output
intermediate
2:00remaining
What happens when deleting from an empty doubly linked list?
Given an empty doubly linked list, what is the output after calling delete_from_beginning and then printing the 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 delete_from_beginning(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(current.data)
            current = current.next
        print(result)

# Setup
empty_dll = DoublyLinkedList()

# Delete first node
empty_dll.delete_from_beginning()

# Print list
empty_dll.print_list()
A[]
B[None]
C[0]
DError
Attempts:
2 left
💡 Hint
Deleting from an empty list should not change anything.
🔧 Debug
advanced
2:00remaining
Identify the error in delete_from_beginning method
What error will this code raise when deleting from a list with only one node?
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 delete_from_beginning(self):
        if not self.head:
            return
        self.head = self.head.next
        self.head.prev = None

# Setup
single_node_list = DoublyLinkedList()
single_node_list.head = Node(100)

# Delete first node
single_node_list.delete_from_beginning()
AAttributeError
BNo error
CIndexError
DNone
Attempts:
2 left
💡 Hint
Check what happens when head.next is None and you try to access head.prev.
❓ Predict Output
advanced
2:00remaining
What is the list after deleting from beginning twice?
Given a doubly linked list 5 <-> 15 <-> 25 <-> 35, what is the list after deleting from beginning two times?
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_from_beginning(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(current.data)
            current = current.next
        print(result)

# Setup
dll = DoublyLinkedList()
dll.append(5)
dll.append(15)
dll.append(25)
dll.append(35)

# Delete twice
dll.delete_from_beginning()
dll.delete_from_beginning()

# Print list
dll.print_list()
A[15, 25, 35]
B[35]
C[25, 35]
D[5, 15, 25, 35]
Attempts:
2 left
💡 Hint
Each deletion removes the head node and updates the head pointer.
🧠 Conceptual
expert
1:00remaining
What is the time complexity of deleting from the beginning of a doubly linked list?
Choose the correct time complexity for deleting the first node in a doubly linked list of size n.
AO(log n)
BO(n)
CO(n^2)
DO(1)
Attempts:
1 left
💡 Hint
Deleting from beginning only changes a few pointers.

Practice

(1/5)
1. What happens to the head pointer when you delete the first node from a doubly linked list?
easy
A. It moves to the second node in the list
B. It becomes null immediately
C. It points to the last node in the list
D. It remains pointing to the deleted node

Solution

  1. Step 1: Understand the head pointer role

    The head pointer always points to the first node in the list.
  2. Step 2: After deleting the first node

    The head must move to the next node, which becomes the new first node.
  3. Final Answer:

    It moves to the second node in the list -> Option A
  4. Quick Check:

    Head points to new first node after deletion [OK]
Hint: Head always points to the first node, update after deletion [OK]
Common Mistakes:
  • Not updating head pointer after deletion
  • Setting head to null even if list has more nodes
  • Leaving head pointing to deleted node
2. Which of the following is the correct syntax to delete the first node in a doubly linked list in Python?
easy
A. head = None; head.next = None
B. head.prev = None; head = head.next
C. head = head.next; head.prev = None
D. head.next = None; head = head.prev

Solution

  1. Step 1: Move head to next node

    Assign head = head.next to move head forward.
  2. Step 2: Remove backward link

    Set head.prev = None to disconnect previous first node.
  3. Final Answer:

    head = head.next; head.prev = None -> Option C
  4. Quick Check:

    Update head then clear prev link [OK]
Hint: Update head first, then clear prev pointer [OK]
Common Mistakes:
  • Setting prev before moving head
  • Trying to set next of head to None incorrectly
  • Setting head to None without updating
3. What will be the output linked list after deleting the first node from this doubly linked list?
Initial list: 10 <-> 20 <-> 30 <-> null

Code snippet:
head = head.next
head.prev = None
medium
A. 20 <-> 30 <-> null
B. 10 <-> 30 <-> null
C. 30 <-> null
D. null

Solution

  1. Step 1: Move head to second node

    Head moves from 10 to 20.
  2. Step 2: Remove backward link of new head

    Set 20.prev = None, disconnecting 10.
  3. Final Answer:

    20 <-> 30 <-> null -> Option A
  4. Quick Check:

    First node removed, list starts at 20 [OK]
Hint: After deletion, head points to second node [OK]
Common Mistakes:
  • Forgetting to set prev to None
  • Incorrectly skipping nodes
  • Outputting original list without change
4. Identify the error in this code snippet for deleting the first node of a doubly linked list:
if head is not None:
    head = head.next
    head.prev = None
medium
A. No error, code is correct
B. head.prev = None causes error if head.next is None
C. head = head.next should be after setting prev to None
D. Missing deletion of the old head node

Solution

  1. Step 1: Check head.next for None

    If head.next is None, head becomes None after assignment.
  2. Step 2: Setting head.prev causes error

    Trying to set head.prev = None when head is None causes AttributeError.
  3. Final Answer:

    head.prev = None causes error if head.next is None -> Option B
  4. Quick Check:

    Check for None before setting prev [OK]
Hint: Check if head is None before setting head.prev [OK]
Common Mistakes:
  • Not checking if head.next is None
  • Assuming list has more than one node always
  • Ignoring possible AttributeError
5. Given a doubly linked list with nodes: 5 <-> 15 <-> 25 <-> 35 <-> null, after deleting the first node twice, what will be the resulting list?
hard
A. null
B. 15 <-> 25 <-> 35 <-> null
C. 35 <-> null
D. 25 <-> 35 <-> null

Solution

  1. Step 1: First deletion moves head from 5 to 15

    After first deletion, list is 15 <-> 25 <-> 35 <-> null.
  2. Step 2: Second deletion moves head from 15 to 25

    After second deletion, list is 25 <-> 35 <-> null.
  3. Final Answer:

    25 <-> 35 <-> null -> Option D
  4. Quick Check:

    Two deletions remove first two nodes [OK]
Hint: Each deletion moves head to next node [OK]
Common Mistakes:
  • Deleting only once instead of twice
  • Not updating head correctly after each deletion
  • Confusing node values or order
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