-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.py
51 lines (51 loc) · 1.48 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
# 21. Merge two sorted lists
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def mergeTwoLists(self, list1, list2):
"""
:type list1: Optional[ListNode]
:type list2: Optional[ListNode]
:rtype: Optional[ListNode]
"""
cur1 = list1
cur2 = list2
list3 = None
if cur1:
if cur2:
if cur1.val > cur2.val:
list3 = cur2
cur2 = cur2.next
else:
list3 = cur1
cur1 = cur1.next
else:
list3 = cur1
cur1 = cur1.next
else:
if cur2:
list3 = cur2
cur2 = cur2.next
cur3 = list3
while cur1 or cur2:
if cur1 and (not cur2):
cur3.next = cur1
cur1 = cur1.next
cur3 = cur3.next
elif (not cur1) and cur2:
cur3.next = cur2
cur2 = cur2.next
cur3 = cur3.next
else:
if cur1.val > cur2.val:
cur3.next = cur2
cur2 = cur2.next
cur3 = cur3.next
else:
cur3.next = cur1
cur1 = cur1.next
cur3 = cur3.next
return list3