Amazon interview question

reverse linked list

Interview Answer

Anonymous

1 Sept 2011

various ways to do it. I like to use a stack. basically : iterate thru linkedist, push in stack. construct new linkedlist from poping sttack. ================= LinkedList reverse = new Linkedlist(); for(Iterator it = list.iterator(); it.hasNext();) { stack.push(it.next()); } //constructing reverse list from poping the stack while (! stack.empty()) { reverse.setNext(stack.pop()); } Not the most effective way, but trust me they like answers like that. you have showed how to use a datastructure