Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions missing-number/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Missing Number
class Solution:
def missingNumber(self, nums: List[int]) -> int:
# First Solution: Time Complexity: O(n^2)
n = len(nums)

for i in range(0,n + 1):
if i not in nums:
return i

# Time Complexity: O(n)
n = len(nums)
# calculate the sum of first n numbers
sum_val = n * (n + 1) // 2
return sum_val - sum(nums)

42 changes: 42 additions & 0 deletions reorder-list/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
# Time Complexity: O(N)
# The order:
# 0, n, 1, n -1, 2, n-3 ...
# first Idea
# need to save the index of the original head
# Hashmap: iterate through the head until head it none, save index as key and head.val as value
head_hash = {}
temp = head
index = 0
length = 0
# Save index and value in the hashmap
while temp is not None:
head_hash[index] = temp.val
temp = temp.next
index += 1
length += 1
# reset index to 0, and use it to iterate through the head again
index = 0
# to keep track of n-1, n-2, n-3 ...
count = 1
# Iterate through the head again and change the value based on the index
while head is not None:
res = index % 2
# if the index is even number
if res == 0:
head.val = head_hash[index/2]
# n, n-1, n-2 when the index is odd
else:
head.val = head_hash[length - count]
count += 1
index += 1
head = head.next