Lesson 6 of 14
Pop Front
Removing the First Node
Removing the head is O(1): advance the head pointer past the first node and free the old one.
struct Node *pop_front(struct Node *head) {
if (head == NULL) return NULL;
struct Node *old = head;
head = head->next;
free(old);
return head;
}
Before: [1] -> [2] -> [3] -> NULL
After: [2] -> [3] -> NULL (pop_front)
Always Check for NULL
If the list is empty (head == NULL), return NULL immediately — don't dereference a null pointer.
Memory
Save the old head pointer before advancing, then free it. Without free, the removed node leaks memory.
Return the New Head
The caller's head must be updated:
head = pop_front(head);
Your Task
Implement struct Node *pop_front(struct Node *head) that removes the first node and returns the new head.
TCC compiler loading...
Loading...
Click "Run" to execute your code.