![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution: def reversePrint(self, head: ListNode) -> List[int]: newList=[] while head: newList.append(head.val) head=head.next return newList[::-1]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution: def reversePrint(self, head: ListNode) -> List[int]: newList=[] if head != None: newList.append(head.val) head = head.next while head !=None: newList.append(head.val) head=head.next newList.reverse() return newList
利用递归: 先走至链表末端,回溯时依次将节点值加入列表 ,这样就可以实现链表值的倒序输出。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
递推阶段: 每次传入 head.next ,以 head == None(即走过链表尾部节点)为递归终止条件,此时返回空列表 [] 。 回溯阶段: 利用 Python 语言特性,递归回溯时每次返回 当前 list + 当前节点值 [head.val] ,即可实现节点的倒序输出。 作者: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) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 class Solution: def reversePrint(self, head: ListNode) -> List[int]: return self.reversePrint(head.next) + [head.val] if head else []
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class CQueue: def __init__(self): self.stack1=[] self.stack2=[] def appendTail(self, value: int) -> None: self.stack1.append(value) def deleteHead(self) -> int: if self.stack2: return self.stack2.pop() elif not self.stack1: return -1 else: while self.stack1: self.stack2.append(self.stack1.pop()) return self.stack2.pop()
使用二分查找的方式:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution: def minArray(self, numbers: [int]) -> int: i, j = 0, len(numbers) - 1 while i < j: m = (i + j) // 2 if numbers[m] > numbers[j]: i = m + 1 elif numbers[m] < numbers[j]: j = m else: j -= 1 return numbers[i]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution: def minArray(self, numbers: [int]) -> int: return (min(numbers))
本文来自博客园,作者:topass123,转载请注明原文链接:https://www.cnblogs.com/topass123/p/12557236.html