Cisco Interview Question

Print the elements in a linked list in reverse order.

Interview Answer

Anonymous

Nov 11, 2012

Assume that it is a singly linkedList void PrintElements(LinkedList list){ if (list.size() == 0){return;} cursor = list.head.next ;// (the lst item in the list) stack = new Stack() ;// (a LIFO stack) while(cursor.hasNext()){ stack.push(cursor.getItem()); cursor = cursor.next; } stack.push(cursor.getItem()); //for printing the elements while(stack.size() >0){ System.out.print(stack.pop()); } }