-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge_two_sorted_lists.py
44 lines (39 loc) · 1.39 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
# 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, l1: ListNode, l2: ListNode) -> ListNode:
# the edge case(s):
if l1 == None:
return l2
if l2 == None:
return l1
# we insert a 'dummy head' before the new list
dummy_node = ListNode(None)
# new linked list
current_pointer= dummy_node
# current_pointer -> dummy_node
while (l1 != None) and (l2 != None):
# case 1
if l1.val <= l2.val:
current_pointer.next = l1
l1 = l1.next
current_pointer = current_pointer.next
# current_pointer.next -> l1
# l1 -> l1.next
# current_pointer -> l1
# case 2
else:
current_pointer.next = l2
l2 = l2.next
current_pointer = current_pointer.next
# current_pointer.next -> l2
# l2 -> l2.next
# current_pointer -> l2
if l1 == None:
current_pointer.next = l2
if l2 == None:
current_pointer.next = l1
return dummy_node.next