码迷,mamicode.com
首页 > 其他好文 > 详细

剑指Offer03

时间:2019-07-06 23:16:54      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:nod   class   list   aof   剑指offer   head   pre   tac   java   

package javaOffer;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;


public class o03_ListFromTailtoHead_03 {


public class ListNode{
public int val;
public ListNode next=null;

public ListNode(int val) {
this.val = val;
}
}

//递归方式
ArrayList arrayList=new ArrayList();
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if(listNode!=null){
printListFromTailToHead(listNode.next);
arrayList.add(listNode.val);
}

return arrayList;
}

//非递归模式
public ArrayList<Integer> printListFromTailToHeadStack(ListNode listNode){
Stack<Integer> stack=new Stack<>();
while(listNode!=null){
stack.push(listNode.val);
listNode=listNode.next;
}

ArrayList arrayList=new ArrayList();
while(stack!=null){
arrayList.add(stack.pop());
}
return arrayList;
}
}

剑指Offer03

标签:nod   class   list   aof   剑指offer   head   pre   tac   java   

原文地址:https://www.cnblogs.com/fanzihao/p/11144589.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!