diff --git a/merge-two-sorted-lists/rivkode.py b/merge-two-sorted-lists/rivkode.py new file mode 100644 index 000000000..6cff0521e --- /dev/null +++ b/merge-two-sorted-lists/rivkode.py @@ -0,0 +1,53 @@ +class ListNode(object): + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution(object): + def mergeTwoLists(self, list1, list2): + """ + :type list1: Optional[ListNode] + :type list2: Optional[ListNode] + :rtype: Optional[ListNode] + """ + # 더미 노드 생성 + dummy = ListNode(-1) + current = dummy + + # 두 리스트를 순회하며 병합 + while list1 and list2: + if list1.val <= list2.val: + current.next = list1 + list1 = list1.next + else: + current.next = list2 + list2 = list2.next + current = current.next + + # 남아 있는 노드 처리 + if list1: + current.next = list1 + elif list2: + current.next = list2 + + # 더미 노드 다음부터 시작 + return dummy.next + +if __name__ == "__main__": + solution = Solution() + + # test case + list1 = ListNode(1, ListNode(2, ListNode(4, ))) + list2 = ListNode(1, ListNode(3, ListNode(4, ))) + + result = solution.mergeTwoLists(list1, list2) + + while result is not None: + print(result.val) + result = result.next + + + + + + diff --git a/missing-number/rivkode.py b/missing-number/rivkode.py new file mode 100644 index 000000000..43e1ac29d --- /dev/null +++ b/missing-number/rivkode.py @@ -0,0 +1,38 @@ +class Solution(object): + # 시간복잡도 nlog(n) sort + # 공간복잡도 n 정렬시 + def missingNumber_1(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + sort_nums = sorted(nums) + + v = 0 + for i in sort_nums: + if v != i: + return v + else: + v += 1 + + return v + + # hash를 사용 + # 모든 숫자를 dict에 입력 + # for문을 돌면서 해당 hash가 dict에 존재하는지 체크 + # 시간복잡도 n - for문 + # 공간복잡도 n - dict 생성 + def missingNumber(self, nums): + hash_keys = dict() + for i in range(len(nums) + 1): + hash_keys[i] = 0 + for i in nums: + hash_keys[i] = 1 + + for i in range(len(nums) + 1): + if hash_keys[i] != 1: + return i + + return 0 + +