当前位置:网站首页>剑指 Offer 06.从头到尾打印链表
剑指 Offer 06.从头到尾打印链表
2022-07-05 05:26:00 【ThE wAlkIng D】
题目描述

问题解析
1.由于输入输出都是数组,所以新建一个数组,用来存放反转以后的数组即从尾部到头的数组
2.其次取出数组的长度赋值给新数组
3.头结点取值赋值给新数组,然后往后遍历,
代码实例
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public int[] reversePrint(ListNode head) {
ListNode node = head;
int len = 0;
while(node != null){
len++;
node = node.next;//取出数组原始长度
}
int[] nums = new int[len];
while(head != null){
nums[--len] = head.val;//数组从头到尾依次存储每个节点的值
head = head.next;
}
return nums;
}
}
/** * Your CQueue object will be instantiated and called as such: * CQueue obj = new CQueue(); * obj.appendTail(value); * int param_2 = obj.deleteHead(); */
}
}
边栏推荐
- 2022上半年全国教师资格证下
- [speed pointer] 142 circular linked list II
- [to be continued] [UE4 notes] L1 create and configure items
- A preliminary study of sdei - see the essence through transactions
- Research on the value of background repeat of background tiling
- Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
- Binary search basis
- Haut OJ 1357: lunch question (I) -- high precision multiplication
- What is the agile proportion of PMP Exam? Dispel doubts
- [turn]: OSGi specification in simple terms
猜你喜欢
![[轉]: OSGI規範 深入淺出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[轉]: OSGI規範 深入淺出

On-off and on-off of quality system construction
![[turn to] MySQL operation practice (I): Keywords & functions](/img/b1/8b843014f365b786e310718f669043.png)
[turn to] MySQL operation practice (I): Keywords & functions

sync. Interpretation of mutex source code

Reverse one-way linked list of interview questions
![[to be continued] [UE4 notes] L2 interface introduction](/img/0f/268c852b691bd7459785537f201a41.jpg)
[to be continued] [UE4 notes] L2 interface introduction

Heap sort summary

小程序直播+电商,想做新零售电商就用它吧!

支持多模多态 GBase 8c数据库持续创新重磅升级
![To be continued] [UE4 notes] L4 object editing](/img/0f/cfe788f07423222f9eed90f4cece7d.jpg)
To be continued] [UE4 notes] L4 object editing
随机推荐
When will Wei Lai, who has been watched by public opinion, start to "build high-rise buildings" again?
Yolov5 ajouter un mécanisme d'attention
Summary of Haut OJ 2021 freshman week
[转]:Apache Felix Framework配置属性
Csp-j-2020-excellent split multiple solutions
[depth first search] 695 Maximum area of the island
Hang wait lock vs spin lock (where both are used)
SAP-修改系统表数据的方法
[to be continued] [depth first search] 547 Number of provinces
Quick sort summary
FVP和Juno平台的Memory Layout介绍
Haut OJ 1347: addition of choice -- high progress addition
kubeadm系列-00-overview
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
[binary search] 69 Square root of X
Download xftp7 and xshell7 (official website)
Haut OJ 1243: simple mathematical problems
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Use of snippets in vscode (code template)
[to be continued] [UE4 notes] L2 interface introduction