File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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
+
9
+ dummy = ListNode (- 1 ) # ๋๋ฏธ ์์ ๋
ธ๋
10
+ current = dummy # ํ์ฌ ์ฐ๊ฒฐ ์์น ํฌ์ธํฐ
11
+
12
+ while list1 and list2 :
13
+
14
+ # ํ์ฌlist1์ ๊ฐ๊ณผ ํ์ฌlist2๊ฐ์ ๋น๊ตํด์ current.next ์ฐ๊ฒฐ
15
+ if list1 .val < list2 .val :
16
+ current .next = list1
17
+ list1 = list1 .next
18
+ else :
19
+ current .next = list2
20
+ list2 = list2 .next
21
+
22
+ # current ๋ค์์ผ๋ก ์ด๋
23
+ current = current .next
24
+
25
+ # ๋ ์ค ํ๋๊ฐ ๋จ์์๋ค๋ฉด ๋๋จธ์ง๋ฅผ ํต์งธ๋ก ๋ถ์ด๊ธฐ(์ผํญ ์ฐ์ฐ์)
26
+ current .next = list1 if list1 else list2
27
+ # if list1:
28
+ # current.next = list1
29
+ # else:
30
+ # current.next = list2
31
+
32
+ return dummy .next
You canโt perform that action at this time.
0 commit comments