题目链接
https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
描述
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
限制:
0 <= 链表长度 <= 10000
示例
输入:head = [1,3,2]
输出:[2,3,1]
初始代码模板
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
}
}
代码
题解评论区代码,思路真是太棒了!
https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/solution/mian-shi-ti-06-cong-wei-dao-tou-da-yin-lian-biao-d/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
int[] res;
int size = 0;
int index = 0;
public int[] reversePrint(ListNode head) {
solve(head);
return res;
}
private void solve(ListNode head) {
if (head == null) {
res = new int[size];
return;
}
size++;
solve(head.next);
res[index++] = head.val;
}
}
该博客分享了LeetCode上“从尾到头打印链表”题目的相关内容,包含题目链接、描述、示例、初始代码模板等,还推荐了题解评论区的代码,认为其思路很棒,并给出了对应题解的链接。
1225





