Skip to content

Commit f9b7d6b

Browse files
committed
merged-two-sorted-lists
1 parent 4e46337 commit f9b7d6b

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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()
9+
merged_list = dummy
10+
11+
while list1 is not None and list2 is not None:
12+
if list1.val <= list2.val:
13+
merged_list.next = list1
14+
list1 = list1.next
15+
else:
16+
merged_list.next = list2
17+
list2 = list2.next
18+
# merged_list ํฌ์ธํ„ฐ๋ฅผ ํ•œ์นธ ์ด๋™์‹œ์ผœ์„œ ๋‹ค์Œ ๋…ธ๋“œ ๋ถ™์ผ ์ค€๋น„
19+
merged_list = merged_list.next
20+
21+
# ๋‚จ์•„ ์žˆ๋Š” ๋…ธ๋“œ ๋ถ™์ด๊ธฐ
22+
merged_list.next = list1 if list1 is not None else list2
23+
24+
return dummy.next

0 commit comments

Comments
ย (0)