-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_two_sorted_lists.py
67 lines (50 loc) · 1.46 KB
/
merge_two_sorted_lists.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
66
67
# 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]
"""
if list1 == None:
return list2
elif list2 == None:
return list1
head = ListNode(-1)
current = head
current1 = list1
current2 = list2
while current1 or current2:
if current1 == None:
current.next = current2
break
elif current2 == None:
current.next = current1
break
if current1.val <= current2.val:
current.next = ListNode(current1.val)
current1 = current1.next
else:
current.next = ListNode(current2.val)
current2 = current2.next
current = current.next
return head.next
def main():
l1 = ListNode(2)
l1.next = ListNode(2)
l1.next.next = ListNode(4)
l2 = ListNode(1)
l2.next = ListNode(3)
l2.next.next = ListNode(4)
s = Solution()
result = s.mergeTwoLists(l1, l2)
# print(result)
while result:
print(result.val)
result = result.next
if __name__ == "__main__":
main()