Skip to content

Commit 783de6a

Browse files
Solve : Merge Two Sorted Lists
1 parent 904339d commit 783de6a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class ListNode:
2+
def __init__(self, val=0, next=None):
3+
self.val = val
4+
self.next = next
5+
6+
class Solution:
7+
def mergeTwoLists(self, list1, list2):
8+
dummy = ListNode()
9+
current = dummy
10+
while list1 and list2:
11+
if list1.val < list2.val:
12+
current.next = list1
13+
list1 = list1.next
14+
else:
15+
current.next = list2
16+
list2 = list2.next
17+
current = current.next
18+
current.next = list1 if list1 else list2
19+
return dummy.next

0 commit comments

Comments
 (0)