Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the Node class with a pointer to the next node.
DSA Python
class Node: def __init__(self, data): self.data = data self.next = [1] self.prev = None
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to 0 or data instead of None.
Confusing next with prev pointer.
✗ Incorrect
The next pointer should be initialized to None because the node does not point to any other node yet.
2fill in blank
mediumComplete the code to initialize the previous pointer in the Node class.
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None self.prev = [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting prev to 0 or data instead of None.
Confusing prev with next pointer.
✗ Incorrect
The prev pointer should be None initially because the node has no previous node yet.
3fill in blank
hardFix the error in the DoublyLinkedList class constructor to initialize the head pointer.
DSA Python
class DoublyLinkedList: def __init__(self): self.head = [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting head to 0 or Node instead of None.
Assigning head to self which is incorrect.
✗ Incorrect
The head pointer should be None initially because the list is empty.
4fill in blank
hardFill both blanks to complete the method that adds a new node at the beginning of the doubly linked list.
DSA Python
def add_at_beginning(self, data): new_node = Node(data) new_node.next = [1] if self.head is not None: self.head.prev = [2] self.head = new_node
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping new_node and self.head in assignments.
Forgetting to update the prev pointer of the old head.
✗ Incorrect
The new node's next points to the current head, and the current head's prev points back to the new node.
5fill in blank
hardFill all three blanks to complete the method that prints the doubly linked list from head to tail.
DSA Python
def print_list(self): current = self.head while current is not [1]: print(current.[2], end=' <-> ') current = current.[3] print('None')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using prev instead of next to move forward.
Checking for current == 0 instead of None.
✗ Incorrect
We loop until current is None, print the data, and move to the next node.