-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.py
65 lines (48 loc) · 1.65 KB
/
21.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'''21. Merge Two Sorted Lists
Created on 2025-01-02 17:01:37
2025-01-02 17:24:40
@author: MilkTea_shih
'''
#%% Packages
from typing import Optional
#%% Variable
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
#%% Functions
# 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, list1: Optional[ListNode],
list2: Optional[ListNode]
) -> Optional[ListNode]:
result: ListNode = ListNode()
result_head = result #record the head of `result`
while list1 is not None and list2 is not None:
number: int
if list1.val < list2.val: #get a smaller node
number = list1.val
list1 = list1.next
else:
number = list2.val
list2 = list2.next
result.next = ListNode(number) #add the smaller node
result = result.next
# Add the rest of the nodes by linking to the remaining list.
result.next = list1 if list2 is None else list2
# Do not do it with travering!! Make good use of the pointer.
#rest_list: Optional[ListNode] = list1 if list2 is None else list2
#while rest_list is not None: #add the rest of nodes
# result.next = ListNode(rest_list.val)
# result = result.next
# rest_list = rest_list.next
return result_head.next
#%% Main Function
#%% Main
if __name__ == '__main__':
pass
#%%