0
0
DSA Pythonprogramming~10 mins

Create and Initialize Doubly Linked List in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Node class with a constructor.

DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.[1] = None
Drag options to blanks, or click blank then click option'
Aprev
Btail
Chead
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' or 'tail' as node attributes instead of 'next'.
Confusing 'prev' and 'next' pointers.
2fill in blank
medium

Complete the code to initialize an empty DoublyLinkedList class.

DSA Python
class DoublyLinkedList:
    def __init__(self):
        self.head = None
        self.[1] = None
Drag options to blanks, or click blank then click option'
Atail
Bdata
Cprev
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' or 'prev' as attributes of the list instead of nodes.
Forgetting to initialize the tail pointer.
3fill in blank
hard

Fix the error in the method to add a node at the beginning of the list.

DSA Python
def add_at_beginning(self, data):
    new_node = Node(data)
    new_node.next = self.head
    if self.head is not None:
        self.head.[1] = new_node
    self.head = new_node
    if self.tail is None:
        self.tail = new_node
Drag options to blanks, or click blank then click option'
Ahead
Bnext
Cprev
Dtail
Attempts:
3 left
💡 Hint
Common Mistakes
Setting 'next' pointer instead of 'prev' on the old head.
Not updating the tail when list was empty.
4fill in blank
hard

Fill both blanks to complete the method that adds a node at the end of the list.

DSA Python
def add_at_end(self, data):
    new_node = Node(data)
    if self.head is None:
        self.head = new_node
        self.tail = new_node
    else:
        self.tail.[1] = new_node
        new_node.[2] = self.tail
        self.tail = new_node
Drag options to blanks, or click blank then click option'
Anext
Bprev
Chead
Dtail
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping 'next' and 'prev' pointers.
Not updating the tail pointer to the new node.
5fill in blank
hard

Fill all three blanks to complete the method that prints the list from head to tail.

DSA Python
def print_list(self):
    current = self.head
    while current is not None:
        print(current.[1], end=' <-> ' if current.[2] is not None else '\n')
        current = current.[3]
Drag options to blanks, or click blank then click option'
Adata
Bnext
Cprev
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to move forward.
Printing 'prev' data instead of current node's data.