视频讲解
代码
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
# Solution -1 Iteration
# # create dummy head
# dummy_head = ListNode(0)
# new_tail = dummy_head
# # keep appending the lowest node to the tail
# while l1 and l2:
# if l1.val > l2.val:
# new_tail.next = l2
# l2 = l2.next
# else:
# new_tail.next = l1
# l1 = l1.next
# new_tail = new_tail.next
# # append unfinished list to the tail
# new_tail.next = l1 if l1 else l2
# return dummy_head.next
# Solution-2: recursion
# edge case
if not l1:
return l2
if not l2:
return l1
# recursion relation.
if l2.val <= l1.val:
l1, l2 = l2, l1
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
PPT讲解
本题涉及的知识点:
我的 Leetcode 讲解频道
代码链接
面试高频考点Youtube链接
- 数组(Array)
- 哈希表(Hash Table)
- 链表(Linked List)
- 数学(Math)
- 链表(Linked List)
- 双指针(Two Pointers)
- 字符串(String)
- 二分查找(Binary Search)
- 分治(Divide and Conquer)
- 动态规划(Dynamic Programming)
- 回溯(Backtracking)
- 栈(Stack)
- 堆(Heap)
- 贪心算法(Greedy)
- 排序(Sort)
- 树(Tree)
- 深度优先搜索(Depth-First Search)
- 广度优先搜索(Breadth-First Search)
- 二叉查找树(Binary Search Tree)
- 递归(Recursion)
- 队列(Queue)
- 移动窗口(Sliding Window)