当前位置:网站首页>Print linked list from end to beginning
Print linked list from end to beginning
2022-07-30 00:15:00 【Ryuzaki Liuhe】
Title:
Enter the head node of a linked list, and return the value of each node from the end to the beginning (returned as an array)
Example 1:
Input: head= [1,3,2]
Output: [2,3,1]
Analysis:
Thinking about creating an array first, you need to determine how many numbers there are,After confirming, it can be loaded directly.
Code:
public class ReversePrint {public int[] reversePrint(ListNodehead) {if (head == null){return new int[0];}ListNode temp = head;int count = 0;//Calculate the length of the returned arraywhile (temp != null){count++;temp = temp.next;}int k = count - 1;int[] res = new int[count];//You can start assigning valueswhile (head !=null){res[k--] = head.val;head = head.next;}return res;}}
边栏推荐
猜你喜欢
随机推荐
I.MX6U-驱动开发-3-新字符驱动
opencv基本图像的滤波
决策树原理及代码实现
YoloV5数据集自动划分训练集、验证集、测试集
call、apply 以及 bind 的区别和用法
1592. 重新排列单词间的空格
Graph Theory: Bipartite Graphs
绘制几何图形
工厂模式
『牛客|每日一题』走迷宫
Decision tree principle and code implementation
EA&UML日拱一卒-状态图::重画按钮状态图
某团实习面经
Music theory & guitar skills
【经验】经验总结-经验教训
vim相关介绍(二)
2022年企业直播行业发展洞察
Worthington酶促细胞收获&细胞粘附和收获
二维数组的查找
Comprehensive Practice - Three-Mison Chess Mini Game