@@ -282,7 +282,6 @@ class Solution:
282
282
```
283
283
- 7 or 9 等于 7
284
284
- None and 7 等于 None
285
- - sorted用在这里为了保证 l1 的值小于等于 l2 的值
286
285
## [ 23. Merge k Sorted Lists 4行] ( https://leetcode.com/problems/merge-k-sorted-lists/ )
287
286
``` python
288
287
# Definition for singly-linked list.
@@ -5523,6 +5522,23 @@ class Solution:
5523
5522
- 空间复杂度:O(logN)
5524
5523
- x^4 = x^2 ** x^2, x^5 = x^2 * x^2 * x, 借此方法可以缩减计算量
5525
5524
5525
+ ☄ ** 总结**
5526
+ #### [ 21. 合并两个有序链表] ( https://leetcode-cn.com/problems/merge-two-sorted-lists/ )
5527
+ ``` python
5528
+ # Definition for singly-linked list.
5529
+ # class ListNode:
5530
+ # def __init__(self, x):
5531
+ # self.val = x
5532
+ # self.next = None
5533
+
5534
+ class Solution :
5535
+ def mergeTwoLists (self , l1 : ListNode, l2 : ListNode) -> ListNode:
5536
+ if l1 and l2:
5537
+ if l1.val > l2.val: l1, l2 = l2, l1
5538
+ l1.next = self .mergeTwoLists(l1.next, l2)
5539
+ return l1 or l2
5540
+ ```
5541
+
5526
5542
# 常用技巧总结
5527
5543
- set 中的 in 操作时间复杂度为 O(1)
5528
5544
- dict.get 可以设置预设值,避免取到不存在的 key 时报错
0 commit comments