Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a linked list node with a value.
DSA Python
class Node: def __init__(self, data): self.data = data self.[1] = None
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' for a singly linked list node.
✗ Incorrect
The next attribute points to the next node in the linked list.
2fill in blank
mediumComplete the code to add a new node at the beginning of the linked list.
DSA Python
def add_at_head(head, data): new_node = Node(data) new_node.[1] = head return new_node
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting 'prev' instead of 'next' causes errors in singly linked lists.
✗ Incorrect
We set the new node's next to the current head to link it properly.
3fill in blank
hardFix the error in the code to traverse and print all node values in the linked list.
DSA Python
def print_list(head): current = head while current is not None: print(current.data) current = current.[1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' or 'head' instead of 'next' causes infinite loops or errors.
✗ Incorrect
To move forward through the list, update current to current.next.
4fill in blank
hardComplete the code to add a new node at the end of the linked list.
DSA Python
def add_at_tail(head, data): new_node = Node(data) if head is None: return new_node current = head while current.[1] is not None: current = current.next current.[2] = new_node return head
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' or assuming a 'tail' pointer in a basic singly linked list.
✗ Incorrect
Traverse to the last node using current.next and then link the new node with current.next = new_node.
5fill in blank
hardComplete the code to reverse the linked list iteratively.
DSA Python
def reverse(head): prev = None curr = head while curr is not None: next_temp = curr.[1] curr.[2] = prev prev = curr curr = next_temp return prev
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' for saving next_temp or forgetting to save it causes data loss.
✗ Incorrect
Save curr.next in next_temp before setting curr.next = prev to reverse the direction.