Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' or 'tail' as node attributes instead of 'next'.
Confusing 'prev' and 'next' pointers.
✗ Incorrect
The 'next' attribute points to the next node in the doubly linked list.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' or 'prev' as attributes of the list instead of nodes.
Forgetting to initialize the tail pointer.
✗ Incorrect
The 'tail' attribute points to the last node in the doubly linked list.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting 'next' pointer instead of 'prev' on the old head.
Not updating the tail when list was empty.
✗ Incorrect
When adding at the beginning, the old head's 'prev' pointer should point to the new node.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping 'next' and 'prev' pointers.
Not updating the tail pointer to the new node.
✗ Incorrect
The current tail's 'next' points to the new node, and the new node's 'prev' points back to the old tail.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to move forward.
Printing 'prev' data instead of current node's data.
✗ Incorrect
We print the 'data' of current node, check if 'next' exists to print arrow, and move to 'next' node.