博客
关于我
剑指offer从头打印链表
阅读量:637 次
发布时间:2019-03-14

本文共 584 字,大约阅读时间需要 1 分钟。

从头打印链表

题目描述

在这里插入图片描述

分析

题目要求从头到尾反向输出每个节点的值(并用数组返回),看到官方的题解是用栈来解决的(栈先进后出的特点),但自己目前还没有接触到,所以先用一个时间复杂度和空间复杂度复杂的方法吧,之后学到了再补充。

  • 第一步,先遍历链表求出链表长度len
  • 第二步,new一个len大小的数组
  • 第三步,将结点的每一个值都存到数组中(注意,从后往前存
  • 返回数组即可

AC代码

class Solution {       public int[] reversePrint(ListNode head) {               ListNode cur=head;        int len=0;        while(cur!=null)        {               len++;            cur=cur.next;        }        cur=head;        int[] arr=new int[len];        while(cur!=null)        {              arr[len-1]=cur.val;           len--;           cur=cur.next;        }        return arr;    }}

转载地址:http://rrhoz.baihongyu.com/

你可能感兴趣的文章
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>