Title Description:
Enter a list to print the values of each node of the list from the end of the head.
Input Description:
input as linked list the table header
Output Description:
output as a "new linked list" header that needs to be printed
Stack processing:
/*** public class listnode {* int val;* listnode next = null;** listnode (Int val) {* this.val = val;* }* }**/import java.util.Stack;import java.util.ArrayList;public class solution { public arraylist<integer> Printlistfromtailtohead (Listnode listnode) { Stack<Integer> Sk=new stack<integer> (); while (listNode!=null) { sk.push (listnode.val); listnode=listnode.next; } ArrayList<Integer> Al=new arraylist<integer> (); while (!sk.isEmpty ()) { al.add (Sk.pop ()); } return al; }}
recursive:
/*** public class listnode {* int val;* listnode next = null;** listnode (Int val) {* this.val = val;* }* }**/import java.util.arraylist;public class solution { public arraylist<integer> printlistfromtailtohead (ListNode ListNode) { ArrayList<Integer> list=new Arraylist<integer> (); listnode pnode=listnode; if (PNode!=null) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp; if (pnode.next!=null) { list=printlistfromtailtohead (PNode.next); } list.add (Pnode.val); } return list; }}
Enter a linked list to print the value of each node of the list from the end of the head