Note: (1) This shows the difference between Java data structure and C language.
(2) Stack operation is carried out directly using stack.
1 PackageCom.xsf.SordForOffer;2 3 ImportJava.util.Stack;4 5 6 /*7 * Sword means offer8 *pro5 linked list reverse output9 * */Ten One classlistnode{ A //Defining Nodes - intdata; - ListNode Next; the } - - //reverse printing with stacks - classprintlistreverse{ + //Enter a head node and then use the stack operation to complete the reverse output. - Public voidPrint_reverse (ListNode listnode) { +stack<listnode> stack =NewStack<listnode>(); A //to put a node into the stack at while(listnode!=NULL){ - Stack.push (listnode); -ListNode =Listnode.next; - } - //node out Stack - while(!Stack.empty ()) { in System.out.println (Stack.pop (). data); - } to } + - } the Public classPro5linklistreverse { * Public Static voidMain (string[] args) { $ListNode Node1 =NewListNode ();Panax NotoginsengListNode Node2 =NewListNode (); -ListNode Node3 =NewListNode (); theNode1.data = 1; +Node2.data = 3; ANode3.data = 5; theNode1.next =Node2; +Node2.next =Node3; -Printlistreverse test =Newprintlistreverse (); $ Test.print_reverse (node1); $ } -}
Pro5 Print list from beginning to end (Java)