SeawayLee

北方工业大学/CS/硕士在读

  • 主页
  • 所有文章
  • 书单
标签分类 友链 关于我

SeawayLee

北方工业大学/CS/硕士在读

  • 主页
  • 所有文章
  • 书单

剑指Offer_5_逆序打印链表值

2017-03-15

题目描述

输入一个链表的头结点,从未到头反过来打印出每个节点的值。

解题思路

  • 可以先将正序输出结果压入栈或双向队列,再从栈顶输出。FILO
  • 使用递归,从结尾开始输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class PrintListReversingly
{
/**
* 递归实现
* @Author NikoBelic
* @Date 15/03/2017 21:25
*/
private static void printReversingly(SimpleLinkedList head)
{
if (head == null)
return;
if (head.next != null)
printReversingly(head.next);
System.out.print(head.val + " ");
}
/**
* 栈或队列实现
* @Author NikoBelic
* @Date 15/03/2017 21:28
*/
private static void printByQueue(SimpleLinkedList head)
{
Deque queue = new LinkedBlockingDeque();
while (head != null)
{
queue.addFirst(head.val);
head = head.next;
}
while (queue.size() > 0)
{
System.out.print(queue.pop() + " ");
}
}
public static void main(String[] args)
{
SimpleLinkedList head = new SimpleLinkedList(0, null);
SimpleLinkedList p;
p = head;
for (int i = 1; i <= 10; i++)
{
p.next = new SimpleLinkedList(i, null);
p = p.next;
System.out.print(i + " ");
}
System.out.println("\n递归逆序输出结果");
printReversingly(head.next);
System.out.println("\n栈逆序输出结果");
printByQueue(head.next);
}
}
赏

晚饭加个鸡腿 ^.^

支付宝
微信
  • 剑指Offer

扫一扫,分享到微信

微信分享二维码
剑指Offer_6_重建二叉树
String类深入解析
Like Issue Page
Error: Not Found
Login with GitHub
Styling with Markdown is supported
Powered by Gitment
© 2018 SeawayLee
Hexo Theme Yilia by Litten
  • 标签分类
  • 友链
  • 关于我

tag:

  • JavaWeb
  • Java基础
  • Linux
  • Docker
  • Mac工具
  • Git
  • PythonWeb
  • Celery
  • Mysql
  • Flask
  • Nosql
  • Oracle
  • Python基础
  • 工具
  • 搜索
  • 面经
  • 高并发
  • Spring
  • Java
  • NIO
  • JVM
  • 多线程
  • Redis
  • 大数据
  • Hive
  • Storm
  • 大数据基础
  • HBase
  • Hadoop
  • 剑指Offer
  • 面试
  • 爬虫
  • Java爬虫
  • 算法
  • Python

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • Github
  • 知乎
  • CSDN
SeawayLee.
Male | 1993 | BeiJing
NCUT | CS | Postgraduate(2015-2018)

Dream To Be A Top Programmer.
Just Shut Up And Show Me Your Code.