当前位置:网站首页>剑指 Offer 06. 从尾到头打印链表
剑指 Offer 06. 从尾到头打印链表
2022-06-22 20:52:00 【前端粉刷匠】
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
例子:
输入:head = [1,3,2]
输出:[2,3,1]
解法1:
思路:将head数组变成一个链表,然后将链表的头输入。每次将第一个插入到数组的第一个最前面。考察的知识点就是链表的逆置,突破点在与先找到最后一个节点,然后从倒数第二个开始逆转。
let head = [1,3,2]
// 创建链表
function ListNode(val){
this.val = val;
this.next = null;
}
let a = new ListNode();
let b = new ListNode();
let c = new ListNode();
a.val = head[0];
a.next = b;
b.val = head[1];
b.next = c;
c.val = head[2];
c.next = null;
// 反转数组
function reversePrint(head){
let arr = []
while(head != null){
arr.unshift(head.val)
head = head.next;
}
return arr;
}
解法2:
思路:将链表中的数据保存到数组中,然后使用数组的reverse()方法逆转
function reversePrint(head){
let arr = []
while(head != null){
arr.push(head.val)
head = head.next;
}
return arr.reverse();
}
暂时想多这么多~ 如果大家还有好方法,欢迎推荐~~
七夕快乐~~
边栏推荐
- Which securities company is the safest and best choice for stock trading account opening
- flink同步mysql数据到ES
- Atcoder abc256 full problem solution (interval merging template, matrix fast power optimization DP, line segment tree...)
- leetcode. 11 --- container with the most water
- . Net 5.0 realizes the source code analysis of the oidc authentication part of single sign on through identityserver4
- Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization
- Grafana report display of sentinel based high availability current limiting system
- 一个spark app demo
- Introduction and example application of PostgreSQL string separator function (regexp\u split\u to\u table)
- 别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
猜你喜欢

Practice brings true knowledge: the strongest seckill system architecture in the whole network is decrypted. Not all seckills are seckills!!

5 minutes to quickly launch web applications and APIs (vercel)

three.js模拟驾驶游览艺术展厅---打造超级相机控制器

2021-04-05
Mysql8 installation and environment configuration

How to continuously improve performance| DX R & D mode

In the middle of the year, we will promote the integration of worry free, and the value-added package will be reduced by 6

别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!

The relationship between derivative and differential of function

MySQL master-slave synchronization and its basic process of database and table division
随机推荐
Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
2021-08-21
Summary of just meal with 900W increase in playback and acclaim from station B users
2021-04-16
MySQL constraints
SqlServer 复制表的自增属性
In the third week of June, the main growth ranking list (BiliBili platform) of station B single feigua data up was released!
A case of misuse of append
Fundamentals of shell programming (Part 7: branch statement -if)
2021-05-02
[redisson] source code analysis of multilock
【Kubernetes 系列】Kubernetes 概述
2021-03-06
Mysql database design
What if the SQL execution plan of the production system suddenly becomes worse?
Introduction and example application of PostgreSQL string separator function (regexp\u split\u to\u table)
SQL performance optimization method for interval retrieval
The xinjietu x70s has been listed for 87900 times and has leapfrogged the class in space safety. It is worthy of being a 7-seat SUV of the National University of China
Mysql database DQL exercise
2020-12-04