Print the elements in a linked list in reverse order.
Anonymous
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()); } }
Check out your Company Bowl for anonymous work chats.