diff --git a/du5/snake.c b/du5/snake.c index 88ef308..ead5e0f 100644 --- a/du5/snake.c +++ b/du5/snake.c @@ -22,22 +22,22 @@ struct snake* add_snake (struct snake* snake, int x, int y) { } struct snake* remove_snake(struct snake* snake) { - if(snake == NULL || snake-> next == NULL) { + if (snake == NULL) { + return NULL; + } + + if (snake->next == NULL) { free(snake); return NULL; } - struct snake* previous = NULL; - struct snake* current = NULL; - while (current->next !=NULL) { - previous = current; - current = current->next; + struct snake* temp = snake; + while (temp->next->next != NULL) { + temp = temp->next; } - if (previous !=NULL) { - free(current->next); - previous->next = NULL; - } + free(temp->next); + temp->next = NULL; return snake; } diff --git a/du5/snake.o b/du5/snake.o index c417a44..7632eef 100644 Binary files a/du5/snake.o and b/du5/snake.o differ