Question d’entretien chez Amazon

How would you reverse a linked list?

Réponses aux questions d'entretien

Utilisateur anonyme

19 mars 2012

For a singly-linked list consiting of n linked nodes... i -2) { node[i+2].next <-- &node[i+1]; i = i - 1; } node[0].next <-- null;

2

Utilisateur anonyme

29 mars 2012

dave, the tricky answer given below is not acceptable in interview as well as in real programming. Please dont take these words as negative but try formulating solutions which are simple to understand and elegant. for example the invariants in above code and termination condition of -2 is not good. for example you can write -- Node* prev = &head; Node* curr = prev->next; while(curr) { Node* tmp = curr->next; curr->next = prev; prev=curr; curr = tmp; } return prev;

3

Utilisateur anonyme

19 mars 2012

This tutorial answers your question: http://youtu.be/LgapVjJYxqc