Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Insert at End of Doubly Linked List 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
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.

Practice

(1/5)
1. What is the main purpose of inserting a node at the end of a doubly linked list?
easy
A. To reverse the list
B. To add a new element after the last node
C. To remove the first node
D. To find the middle node

Solution

  1. Step 1: Understand the structure of a doubly linked list

    A doubly linked list has nodes connected forward and backward, allowing traversal in both directions.
  2. Step 2: Purpose of insertion at the end

    Inserting at the end means adding a new node after the current last node, updating pointers accordingly.
  3. Final Answer:

    To add a new element after the last node -> Option B
  4. Quick Check:

    Insert at end = Add after last node [OK]
Hint: Insert at end means add after last node [OK]
Common Mistakes:
  • Confusing insertion at end with deletion
  • Thinking insertion reverses the list
  • Assuming insertion happens at the start
2. Which of the following is the correct way to update the pointers when inserting a new node at the end of a doubly linked list in Python?
easy
A. new_node.prev = null; new_node.next = head; head.prev = new_node; head = new_node
B. new_node.next = head; new_node.prev = null; head.prev = new_node; head = new_node
C. new_node.next = null; new_node.prev = tail; tail.next = new_node; tail = new_node
D. new_node.prev = tail; new_node.next = tail; tail.next = new_node; tail = new_node

Solution

  1. Step 1: Identify pointer updates for insertion at end

    The new node's next pointer should be null because it will be the last node. Its prev pointer should point to the current tail.
  2. Step 2: Update the tail's next pointer and move tail

    The current tail's next pointer should point to the new node, then update tail to the new node.
  3. Final Answer:

    new_node.next = null; new_node.prev = tail; tail.next = new_node; tail = new_node -> Option C
  4. Quick Check:

    New node next = null, prev = tail [OK]
Hint: New node next is null, prev points to tail [OK]
Common Mistakes:
  • Setting new_node.next to head instead of null
  • Not updating tail pointer after insertion
  • Confusing prev and next pointers
3. What will be the printed output of the doubly linked list after executing the following code?
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

class DoublyLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def insert_end(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = self.tail = new_node
        else:
            self.tail.next = new_node
            new_node.prev = self.tail
            self.tail = new_node

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

dll = DoublyLinkedList()
dll.insert_end(10)
dll.insert_end(20)
dll.insert_end(30)
dll.print_list()
medium
A. 10 -> 20 -> 30 -> null
B. 30 -> 20 -> 10 -> null
C. 10 -> 20 -> null
D. null

Solution

  1. Step 1: Insert nodes 10, 20, 30 at the end

    Each insert_end call adds a new node at the tail, linking nodes in order: 10, then 20, then 30.
  2. Step 2: Print the list from head to tail

    print_list traverses from head, collecting data: 10, 20, 30, then prints with arrows and 'null' at end.
  3. Final Answer:

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

    Insert end adds at tail, print from head [OK]
Hint: Insert end adds at tail; print from head shows order [OK]
Common Mistakes:
  • Confusing print order as reversed
  • Missing last node in output
  • Printing null instead of 'null'
4. Identify the error in the following code snippet that inserts a node at the end of a doubly linked list:
def insert_end(self, data):
    new_node = Node(data)
    if self.head is None:
        self.head = new_node
    else:
        self.tail.next = new_node
        new_node.prev = self.tail
        self.tail = new_node
medium
A. new_node.next should point to head
B. new_node.prev should be null when list is empty
C. self.head should be updated after insertion at end
D. Missing update of tail when list is empty

Solution

  1. Step 1: Check tail update when list is empty

    When the list is empty, only head is set to new_node, but tail is not updated, so tail remains null.
  2. Step 2: Correct tail assignment

    Tail must also be set to new_node when list is empty to keep tail pointer valid.
  3. Final Answer:

    Missing update of tail when list is empty -> Option D
  4. Quick Check:

    Empty list needs tail = new_node too [OK]
Hint: Update tail when list is empty [OK]
Common Mistakes:
  • Not updating tail on empty list
  • Setting new_node.next incorrectly
  • Confusing head and tail updates
5. Given a doubly linked list with nodes containing values [5, 15, 25], you want to insert a new node with value 35 at the end. After insertion, what will be the correct sequence of node values when traversing from head to tail and tail to head respectively?
hard
A. 5 -> 15 -> 25 -> 35 -> null and 35 -> 25 -> 15 -> 5 -> null
B. 35 -> 5 -> 15 -> 25 -> null and 25 -> 15 -> 5 -> 35 -> null
C. 5 -> 15 -> 25 -> null and 35 -> null
D. 5 -> 15 -> 25 -> 35 -> null and 5 -> 15 -> 25 -> 35 -> null

Solution

  1. Step 1: Insert 35 at the end of the list

    The new node with value 35 is added after the current tail (25), updating pointers accordingly.
  2. Step 2: Traverse from head to tail and tail to head

    From head: 5 -> 15 -> 25 -> 35 -> null; From tail: 35 -> 25 -> 15 -> 5 -> null.
  3. Final Answer:

    5 -> 15 -> 25 -> 35 -> null and 35 -> 25 -> 15 -> 5 -> null -> Option A
  4. Quick Check:

    Forward and backward traversal show correct order [OK]
Hint: Forward order adds new node at end; backward reverses [OK]
Common Mistakes:
  • Mixing forward and backward traversal order
  • Forgetting to update prev pointer of new node
  • Assuming new node is added at head
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