Taptu interview question

How do you reverse a linked list?

Interview Answers

Anonymous

17 Jun 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; }

Anonymous

18 Jun 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.