Skip to content

Commit a59af96

Browse files
committed
2 parents e930fad + 69f1937 commit a59af96

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
7+
class Solution:
8+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
9+
start = ListNode()
10+
cur = start
11+
while list1 and list2:
12+
if list1.val <= list2.val:
13+
cur.next = list1
14+
list1 = list1.next
15+
else:
16+
cur.next = list2
17+
list2 = list2.next
18+
cur = cur.next
19+
20+
if list1:
21+
cur.next = list1
22+
elif list2:
23+
cur.next = list2
24+
25+
return start.next
26+
27+
28+

0 commit comments

Comments
 (0)