-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge2SortedArray.py
45 lines (31 loc) · 1.23 KB
/
merge2SortedArray.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
# problem link: https://leetcode.com/problems/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:
def mergeTwoLists(self, list1, list2):
# যেকোনো একটা empty হলে অন্য টা return করতে হবে।
if not list1:
return list2
elif not list2:
return list1
# Initialize the answer list with a dummy node
answer = ListNode()
tmp = answer # by using this we can traverse it otherwise we will lose answer
while list1 and list2: # it will terminate if anyone become None
if list1.val <= list2.val:
tmp.next = list1
list1 = list1.next
else:
tmp.next = list2
list2 = list2.next
tmp = tmp.next
# Append remaining nodes of list1 or list2 to the merged list
if list1:
tmp.next = list1
else:
tmp.next = list2
# Return the merged list starting from the next node of the dummy node
return answer.next