forked from vJechsmayr/PythonAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0021_Merge_Two_Sorted_Lists.py
More file actions
29 lines (25 loc) · 911 Bytes
/
Copy path0021_Merge_Two_Sorted_Lists.py
File metadata and controls
29 lines (25 loc) · 911 Bytes
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
# 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:
current_point = front = ListNode()
while True:
if l1 == None and l2 == None:
break
elif l1 == None:
current_point.next = ListNode(l2.val)
l2 = l2.next
elif l2 == None:
current_point.next = ListNode(l1.val)
l1 = l1.next
elif l1.val <= l2.val:
current_point.next = ListNode(l1.val)
l1 = l1.next
else:
current_point.next = ListNode(l2.val)
l2 = l2.next
current_point = current_point.next
return front.next