Skip to content

Commit ebb2117

Browse files
authored
Create 21-Merge-Two-Sorted-Lists.py
1 parent 5af5fb3 commit ebb2117

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

21-Merge-Two-Sorted-Lists.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
8+
dummy = ListNode()
9+
tail = dummy
10+
11+
while l1 and l2:
12+
if l1.val < l2.val:
13+
tail.next = l1
14+
l1 = l1.next
15+
else:
16+
tail.next = l2
17+
l2 = l2.next
18+
tail = tail.next
19+
20+
if l1:
21+
tail.next = l1
22+
elif l2:
23+
tail.next = l2
24+
25+
return dummy.next

0 commit comments

Comments
 (0)