-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge2SortedLists.py
33 lines (31 loc) · 1.1 KB
/
merge2SortedLists.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
# 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]
"""
sortedList = ListNode()
tail = sortedList
while list1 and list2:
# While list1 and list2 are NOT empty, check which number is higher and append to sortedList
if list1.val < list2.val:
tail.next = list1
list1 = list1.next
tail = tail.next
else:
tail.next = list2
list2 = list2.next
tail = tail.next
# When any ONE of the lists is NULL, the loop ends
if list1:
# If list2 is null, append list1 to the sortedList's tail
tail.next = list1
else:
tail.next = list2
return sortedList.next