剑指 Offer 06. 从尾到头打印链表
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
示例 1:
输入:head = [1,3,2]
输出:[2,3,1]
限制:
0 <= 链表长度 <= 10000
使用栈,每次找到节点值就入栈,节点找完之后就开始出栈。
每弹出一个就加入输出,相当于做一次倒序。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
stack = []
out = []
while head:
stack.append(head.val)
head = head.next
while stack:
out.append(stack.pop())
return out
下面是大佬的递归方法,给力嗷
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
return self.reversePrint(head.next) + [head.val] if head else []
作者:jyd
链接: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/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Java解题代码:
class Solution {
List<Integer> ans = new ArrayList<>();
void dfs(ListNode head){ //使用递归函数进行反转读取
if (head == null) return;
dfs(head.next);
ans.add(head.val);
}
public int[] reversePrint(ListNode head) {
dfs(head);
int len = ans.size();
int[] list = new int[len];
for(int i = 0; i < len; i++) {
list[i] = ans.get(i);
}
return list;
}
}