剑指Offer-5
来源:互联网 发布:mac 终端查看当前目录 编辑:程序博客网 时间:2024/06/08 18:07
题目:
输入一个链表的头节点,从头到尾反过来打印出每个节点的值。
实现
//coding = javaimport java.util.Stack;public class Solution5 { // public static void printListReversinglyUsingStack(Node head){ if(head==null){ return; } Stack<Node> stack = new Stack(); while(head!=null){ stack.push(head); head = head.next; } while (!stack.isEmpty()){ System.out.print(stack.pop().value+" "); } } //递归实现 public static void printListInverselyUsingRecursion(Node head) { if (head == null) { return; } printListInverselyUsingRecursion(head.next); System.out.print(head.value + " "); } public static void main(String[] args){ Node head = new Node(5); head.next = new Node(4); head.next.next = new Node(3); head.next.next.next = new Node(2); head.next.next.next.next = new Node(1); System.out.println("通过栈 实现"); Solution5.printListReversinglyUsingStack(head); System.out.print("\n"); System.out.println("通过递归实现"); Solution5.printListInverselyUsingRecursion(head); }}class Node { int value; Node next; public Node(int value){ this.value=value; } public Node(){ }}
阅读全文
1 0
- 剑指offer chapter 5
- 【1-5】剑指offer
- 【5-10】剑指offer
- 剑指Offer-5
- 剑指Offer读书笔记5
- 剑指offer面试题5
- 剑指offer 面试题5
- 剑指offer-面试题5
- 【面试题】剑指offer 5
- 剑指OFFER纪念版(5)
- 剑指offer-(1-5)
- 剑指OFFER
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- FileWriter和FileReader解析
- 520你表白了没?看看程序员们的花式表白吧!
- elasticsearch中批量的upsert
- Android 动画总结
- 简单工厂模式
- 剑指Offer-5
- Threa.sleep()之学习线程一
- WCF数据协定
- 三阶魔方层先还原方法图解2
- 根据数据库中提供的坐标(经纬度) 在前端地图上标示坐标点、并显示
- HTML动态生成的方法
- logistic Regression & Regularization
- MyBatis之传入多个参数
- 图片情感分析(1):图像数据预处理