File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree 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+ ์๊ฐ๋ณต์ก๋: O(n)
10+ ๊ณต๊ฐ๋ณต์ก๋: O(1)
11+ 1. ๋ ๋ฆฌ์คํธ๋ฅผ ํฉ์ณ์ ์๋ก์ด ๋ฆฌ์คํธ ๋ฐํ
12+ 2. ๋ ๋ฆฌ์คํธ๋ฅผ ๋๋ฉด์ ์์ ๊ฐ์ ์๋ก์ด ๋ฆฌ์คํธ์ ์ถ๊ฐ
13+ 3. ๋ ๋ฆฌ์คํธ๋ฅผ ๋ชจ๋ ์ํํ๋ฉด ๋จ์ ๊ฒ์ ์๋ก์ด ๋ฆฌ์คํธ์ ์ถ๊ฐ
14+ """
15+ root = ListNode (None )
16+ node = root
17+ while list1 and list2 :
18+ if list1 .val < list2 .val :
19+ node .next = list1
20+ list1 = list1 .next
21+ else :
22+ node .next = list2
23+ list2 = list2 .next
24+ node = node .next
25+ node .next = list1 or list2
26+ return root .next
You canโt perform that action at this time.
0 commit comments