0
0
DSA Pythonprogramming~20 mins

Insert at End 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
What is the output after inserting 4 at the end?
Consider a doubly linked list initially containing 1 <-> 2 <-> 3. We insert 4 at the end. 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 self.head is None:
            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(1)
dl.append(2)
dl.append(3)
dl.append(4)
dl.print_list()
A1 <-> 2 <-> 4 <-> 3 <-> null
B1 <-> 2 <-> 3 <-> null
C1 <-> 2 <-> 3 <-> 4 <-> null
D4 <-> 1 <-> 2 <-> 3 <-> null
Attempts:
2 left
💡 Hint
Remember, append adds the new node at the end, so it should come after 3.
Predict Output
intermediate
2:00remaining
What is the output after inserting 10 at the end of an empty list?
We create an empty doubly linked list and insert 10 at the end. 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 self.head is None:
            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.print_list()
A10 <-> null
Bnull
C0 <-> 10 <-> null
D10
Attempts:
2 left
💡 Hint
When the list is empty, the new node becomes the head.
🔧 Debug
advanced
2:00remaining
What error occurs when inserting at end if 'prev' pointer is not set?
In the append method, if we forget to set new_node.prev = last, what error will occur when traversing backwards from the last 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 append(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        # new_node.prev = last  # This line is missing

    def print_reverse(self):
        current = self.head
        if current is None:
            print("null")
            return
        while current.next:
            current = current.next
        result = []
        while current:
            result.append(str(current.data))
            current = current.prev
        print(" <-> ".join(result) + " <-> null")

dl = DoublyLinkedList()
dl.append(1)
dl.append(2)
dl.append(3)
dl.print_reverse()
AAttributeError: 'NoneType' object has no attribute 'prev'
BTypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
CIndexError: list index out of range
DNo error, prints 3 <-> 2 <-> 1 <-> null
Attempts:
2 left
💡 Hint
If prev is not set, it remains None. Accessing prev of None causes an error.
🧠 Conceptual
advanced
1:30remaining
Why is it important to update the 'prev' pointer when inserting at the end?
In a doubly linked list, what is the main reason to update the 'prev' pointer of the new node when inserting at the end?
ATo speed up forward traversal from the head
BTo allow backward traversal from the new node to previous nodes
CTo prevent memory leaks in the list
DTo automatically delete the previous node
Attempts:
2 left
💡 Hint
Think about how doubly linked lists allow moving backwards.
🚀 Application
expert
2:30remaining
What is the list after multiple appends and one manual pointer error?
Given the code below, what is the output of print_list()? Note that after appending 5, the prev pointer of the last node is manually set to None.
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 self.head is None:
            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(1)
dl.append(3)
dl.append(5)
# Manual pointer error
last_node = dl.head
while last_node.next:
    last_node = last_node.next
last_node.prev = None

dl.print_list()
A5 <-> null
B1 <-> 3 <-> null
C1 <-> null
D1 <-> 3 <-> 5 <-> null
Attempts:
2 left
💡 Hint
The print_list method uses next pointers only, so prev pointer changes do not affect forward printing.