当前位置:网站首页>Sword finger offer 06 Print linked list from beginning to end
Sword finger offer 06 Print linked list from beginning to end
2022-07-05 05:28:00 【ThE wAlkIng D】
Title Description

Problem analysis
1. Because the input and output are arrays , So create a new array , It is used to store the array after inversion, that is, the array from tail to head
2. Next, take out the length of the array and assign it to the new array
3. The value of the header node is assigned to the new array , Then go back ,
Code instance
/** * 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;// Fetch the original length of the array
}
int[] nums = new int[len];
while(head != null){
nums[--len] = head.val;// The array stores the values of each node from beginning to end
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(); */
}
}
边栏推荐
猜你喜欢
随机推荐
Romance of programmers on Valentine's Day
Bubble sort summary
Hang wait lock vs spin lock (where both are used)
剑指 Offer 06.从头到尾打印链表
[binary search] 69 Square root of X
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
剑指 Offer 53 - II. 0~n-1中缺失的数字
Zzulioj 1673: b: clever characters???
2022年上半年国家教师资格证考试
对象的序列化
Haut OJ 1357: lunch question (I) -- high precision multiplication
Mysql database (I)
Web APIs DOM node
Pointnet++ learning
[转]: OSGI规范 深入浅出
kubeadm系列-00-overview
SDEI初探-透过事务看本质
远程升级怕截胡?详解FOTA安全升级
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
[allocation problem] 135 Distribute candy


![[speed pointer] 142 circular linked list II](/img/f8/222a360c01d8ef120b61bdd2025044.jpg)






