Print List title description from tail to head
Enter a linked list to print the value of each node of the list from the end of the head
Enter a description
Enter the table header for the linked list
Output description
Output as a "new linked list" header that needs to be printed
Ideas
- One-way linked list is pointed from beginning to end, printing from tail to "LIFO", so we use stack structure stack
Code
Import Java.util.ArrayList;Import Java.util.Stack;public classSolution03 {publicarraylist<Integer> Printlistfromtailtohead (ListNode ListNode) {arraylist<integer> result = new ArrayList<< Span class= "Hljs-type" >integer> (); if (ListNode = null) {return null;} stack Stack = new stack (); while (ListNode! = null) {Stack.add (listnode.val); listnode = Listnode.next;} while (!stack.empty ()) {result.add ((Integer) Stack.pop ()); } return result;} Class listnode {int Val; listnode next = null; listnode (int val) {this.val = val;}}
Print list from tail to end-Sword point offer