Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a linked list is circular by comparing the next pointer of the last node.
DSA C
if (last->next == [1]) { printf("List is circular\n"); } else { printf("List is linear\n"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if last->next is NULL instead of head.
Confusing the last node pointer with the head pointer.
✗ Incorrect
In a circular linked list, the last node's next pointer points back to the head node.
2fill in blank
mediumComplete the code to traverse a circular linked list until it reaches the head again.
DSA C
struct Node* temp = head;
do {
printf("%d -> ", temp->data);
temp = temp->[1];
} while (temp != head); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using prev pointer which may not exist in singly linked lists.
Using incorrect pointer names.
✗ Incorrect
To traverse a linked list, we move from one node to the next using the next pointer.
3fill in blank
hardFix the error in the code that tries to detect a linear linked list by checking if next pointer is NULL.
DSA C
while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->[1]; } printf("NULL\n");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using prev pointer which may not exist.
Using head or tail pointers incorrectly.
✗ Incorrect
To move through a linear linked list, we use the next pointer until it becomes NULL.
4fill in blank
hardFill both blanks to create a function that inserts a new node at the end of a circular linked list.
DSA C
void insertEnd(struct Node** head_ref, int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
if (*head_ref == NULL) {
new_node->next = new_node;
*head_ref = new_node;
return;
}
struct Node* temp = *head_ref;
while (temp->[1] != *head_ref) {
temp = temp->[2];
}
temp->next = new_node;
new_node->next = *head_ref;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using prev pointer which may not exist.
Using incorrect pointer names.
✗ Incorrect
To find the last node in a circular linked list, we move using the next pointer until we reach the head again.
5fill in blank
hardFill all three blanks to create a function that deletes the head node of a circular linked list.
DSA C
void deleteHead(struct Node** head_ref) {
if (*head_ref == NULL) return;
if ((*head_ref)->next == *head_ref) {
free(*head_ref);
*head_ref = NULL;
return;
}
struct Node* temp = *head_ref;
while (temp->[1] != *head_ref) {
temp = temp->[2];
}
struct Node* to_delete = *head_ref;
temp->next = (*head_ref)->[3];
*head_ref = (*head_ref)->next;
free(to_delete);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using prev pointer which may not exist.
Not updating the last node's next pointer.
✗ Incorrect
To delete the head in a circular linked list, find the last node using next pointers, then update its next to the second node.
