package com. my. util;
public class SingleNode {
public int value;
public SingleNode next;
public SingleNode ( int data) {
this . value = data;
}
}
package com. my. offer;
import java. util. ArrayList;
import java. util. Stack;
import com. my. util. SingleNode;
public class Solution03 {
public ArrayList< Integer> printListFromTailToHead1 ( SingleNode head) {
ArrayList< Integer> list = new ArrayList < > ( ) ;
if ( head != null) {
printListFromTailToHead1 ( head. next) ;
list. add ( head. value) ;
}
return list;
}
public ArrayList< Integer> printListFromTailToHead2 ( SingleNode head) {
Stack< Integer> stack = new Stack < > ( ) ;
SingleNode cur = head;
while ( cur != null) {
stack. push ( cur. value) ;
cur = cur. next;
}
ArrayList< Integer> list = new ArrayList < > ( ) ;
while ( ! stack. isEmpty ( ) ) {
list. add ( stack. pop ( ) ) ;
}
return list;
}
}