pvjc24/cv9/snake.c
2024-04-08 15:23:28 +02:00

22 lines
523 B
C

struct snake* remove_snake(struct snake* snake) {
if (snake == NULL) {
return NULL;
}
struct snake* current = snake;
struct snake* prev = NULL;
while (current->next != NULL) {
prev = current;
current = current->next;
}
if (prev != NULL) {
prev->next = NULL;
} else {
// If prev is NULL, it means we are removing the head of the list
// and the new head will be the next node
snake = NULL;
}
free(current);
return snake;
}