Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to remove the first node from the linked list.
DSA Python
def pop_head(head): if head is None: return None new_head = head.[1] return new_head
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to get the new head.
Trying to access 'value' or 'data' instead of the next node.
✗ Incorrect
To remove the first node, we return the next node as the new head.
2fill in blank
mediumComplete the code to pop the last node from a singly linked list.
DSA Python
def pop_tail(head): if head is None or head.next is None: return None current = head while current.[1].next is not None: current = current.next current.next = None return head
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which does not exist in singly linked list nodes.
Trying to access 'value' or 'data' instead of the next node.
✗ Incorrect
We traverse using the 'next' pointer to find the second last node.
3fill in blank
hardFix the error in the code to pop the head node and return its value.
DSA Python
def pop_head_value(head): if head is None: return None, None value = head.[1] head = head.next return value, head
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' which is not defined in the node.
Using 'next' or 'prev' which are pointers, not data.
✗ Incorrect
The node's data is stored in the 'data' attribute, not 'value'.
4fill in blank
hardFill both blanks to pop the last node and return the updated list and popped value.
DSA Python
def pop_tail_value(head): if head is None: return None, None if head.next is None: return None, head.data current = head while current.[1].next is not None: current = current.next value = current.[2].data current.next = None return head, value
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which does not exist in singly linked lists.
Trying to access 'data' directly from current instead of current.next.
✗ Incorrect
We use 'next' to traverse and access the last node's 'data'.
5fill in blank
hardFill all three blanks to pop the head node, return its value, and the updated list.
DSA Python
def pop_head_return(head): if head is None: return None, None value = head.[1] new_head = head.[2] head.[3] = None return value, new_head
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is not present in singly linked list nodes.
Not disconnecting the old head's next pointer.
✗ Incorrect
We get the data, update the head to next, and disconnect the old head's next.