您的位置:首页 > 其它

从尾到头打印链表

2016-04-22 14:47 267 查看
public class ReverseLink {
public class ListNode {
int val;
ListNode next = null;

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

public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> list = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
while (listNode != null) {
stack.push(listNode.val);
listNode = listNode.next;
}
while (!stack.isEmpty()) {
list.add(stack.pop());
}
return list;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: