Skip to content

Commit 80066e8

Browse files
authored
Merge pull request #1371 from yayyz/main
[yayyz] WEEK 04 Solutions
2 parents 4d7221d + 711434b commit 80066e8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

merge-two-sorted-lists/yayyz.py

+25
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, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
8+
dummy = ListNode(0)
9+
current = dummy
10+
11+
while list1 is not None and list2 is not None:
12+
if list1.val < list2.val:
13+
current.next = list1
14+
list1 = list1.next
15+
else:
16+
current.next = list2
17+
list2 = list2.next
18+
current = current.next
19+
20+
if list1 is not None:
21+
current.next = list1
22+
else:
23+
current.next = list2
24+
25+
return dummy.next

0 commit comments

Comments
 (0)