Question d’entretien chez Taptu

How do you reverse a linked list?

Réponses aux questions d'entretien

Utilisateur anonyme

17 juin 2015

node * reverse(node* n) { static node * head; if (n!=NULL) { if (n->next->next != NULL) { head = reverse(n->next); n->next->next = n; n->next = NULL; } else { head = n->next; head->next = n; } } return head; }

Utilisateur anonyme

18 juin 2015

I don't think that is quite right. Consider running the function with NULL. You receive an uninitialized pointer as a result. Consider a one element list, then you pass the initial n is not null test; but you then try to dereference a null pointer on the subsequent if line - which crashes.