File tree Expand file tree Collapse file tree 2 files changed +91
-0
lines changed Expand file tree Collapse file tree 2 files changed +91
-0
lines changed Original file line number Diff line number Diff line change 1+ class ListNode (object ):
2+ def __init__ (self , val = 0 , next = None ):
3+ self .val = val
4+ self .next = next
5+
6+ class Solution (object ):
7+ def mergeTwoLists (self , list1 , list2 ):
8+ """
9+ :type list1: Optional[ListNode]
10+ :type list2: Optional[ListNode]
11+ :rtype: Optional[ListNode]
12+ """
13+ # 더미 노드 생성
14+ dummy = ListNode (- 1 )
15+ current = dummy
16+
17+ # 두 리스트를 순회하며 병합
18+ while list1 and list2 :
19+ if list1 .val <= list2 .val :
20+ current .next = list1
21+ list1 = list1 .next
22+ else :
23+ current .next = list2
24+ list2 = list2 .next
25+ current = current .next
26+
27+ # 남아 있는 노드 처리
28+ if list1 :
29+ current .next = list1
30+ elif list2 :
31+ current .next = list2
32+
33+ # 더미 노드 다음부터 시작
34+ return dummy .next
35+
36+ if __name__ == "__main__" :
37+ solution = Solution ()
38+
39+ # test case
40+ list1 = ListNode (1 , ListNode (2 , ListNode (4 , )))
41+ list2 = ListNode (1 , ListNode (3 , ListNode (4 , )))
42+
43+ result = solution .mergeTwoLists (list1 , list2 )
44+
45+ while result is not None :
46+ print (result .val )
47+ result = result .next
48+
49+
50+
51+
52+
53+
Original file line number Diff line number Diff line change 1+ class Solution (object ):
2+ # 시간복잡도 nlog(n) sort
3+ # 공간복잡도 n 정렬시
4+ def missingNumber_1 (self , nums ):
5+ """
6+ :type nums: List[int]
7+ :rtype: int
8+ """
9+ sort_nums = sorted (nums )
10+
11+ v = 0
12+ for i in sort_nums :
13+ if v != i :
14+ return v
15+ else :
16+ v += 1
17+
18+ return v
19+
20+ # hash를 사용
21+ # 모든 숫자를 dict에 입력
22+ # for문을 돌면서 해당 hash가 dict에 존재하는지 체크
23+ # 시간복잡도 n - for문
24+ # 공간복잡도 n - dict 생성
25+ def missingNumber (self , nums ):
26+ hash_keys = dict ()
27+ for i in range (len (nums ) + 1 ):
28+ hash_keys [i ] = 0
29+ for i in nums :
30+ hash_keys [i ] = 1
31+
32+ for i in range (len (nums ) + 1 ):
33+ if hash_keys [i ] != 1 :
34+ return i
35+
36+ return 0
37+
38+
You can’t perform that action at this time.
0 commit comments