Amazon Interview Question

How would you reverse a linked list?

Interview Answers

Anonymous

Mar 19, 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

Anonymous

Mar 29, 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

Anonymous

Mar 19, 2012

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