-
Notifications
You must be signed in to change notification settings - Fork 0
/
p21.py
41 lines (30 loc) · 1010 Bytes
/
p21.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from typing import Optional
from util.list_node import ListNode
class Solution:
def mergeTwoLists(
list1: Optional[ListNode], list2: Optional[ListNode]
) -> Optional[ListNode]:
dummy = ListNode() # 新链表的头节点,是哑节点
cur = dummy # 新链表的当前节点
# 遍历list1和list2,哪个小就把哪个加入新链表
while list1 is not None and list2 is not None:
if list1.val < list2.val:
cur.next = list1
list1 = list1.next
else:
cur.next = list2
list2 = list2.next
cur = cur.next
# list1和list2可能不等长,拼接剩下的链表
cur.next = list1 if list1 is not None else list2
return dummy.next
def test():
l1 = ListNode(1)
l1.insert(2)
l1.insert(4)
l2 = ListNode(1)
l2.insert(3)
l2.insert(4)
print(Solution.mergeTwoLists(l1, l2))
if __name__ == "__main__":
test()