-
Notifications
You must be signed in to change notification settings - Fork 0
/
python21.py
53 lines (39 loc) · 1.41 KB
/
python21.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
# 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]:
current1 = list1
current2 = list2
final_head = ListNode(0)
if not current1:
final_head = current2
return final_head
elif not current2:
final_head = current1
return final_head
if current1.val > current2.val:
final_head = current2
current2 = current2.next
else:
final_head = current1
current1 = current1.next
final_ptr = final_head
while current1 or current2:
if not current1:
final_ptr.next = current2
return final_head
elif not current2:
final_ptr.next = current1
return final_head
if current1.val > current2.val:
final_ptr.next = current2
current2 = current2.next
final_ptr = final_ptr.next
else:
final_ptr.next = current1
current1 = current1.next
final_ptr = final_ptr.next
return final_head