先学习以下基本操作
21. 合并两个有序链表
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1 is None:
return l2
elif l2 is None:
return l1
elif l1.val < l2.val:
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
else:
l2.next = self.mergeTwoLists(l1, l2.next)
return l2
141. 环形链表
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow,fast=head,head
while fast and fast.next:
slow=slow.next
fast=fast.next.next
if slow == fast:
return True
return False
160. 相交链表
双指针法太绝了

203. 移除链表元素

234. 回文链表
递归为我们提供了一种优雅的方式来反向遍历节点。
真的看不懂,先放这儿吧。
到此结束,链表题有一个重要的递归法没学会。
简洁的代码:
def is_non_decreasing(arr):
return all(arr[i] >= arr[i-1] for i in range(1, len(arr)))
751

被折叠的 条评论
为什么被折叠?



