diff --git a/merge-two-sorted-lists/yoonthecoder.js b/merge-two-sorted-lists/yoonthecoder.js new file mode 100644 index 000000000..7e63fa58c --- /dev/null +++ b/merge-two-sorted-lists/yoonthecoder.js @@ -0,0 +1,25 @@ +var mergeTwoLists = function (list1, list2) { + // create a placeholder node and use it as a starting point + let placeholder = { val: -1, next: null }; + let current = placeholder; + + // loop through the lists until one of them is fully traversed + while (list1 !== null && list2 !== null) { + if (list1.val <= list2.val) { + // connect the element of list1 with the current node + current.next = list1; + // move list1 to its next node + list1 = list1.next; + } else { + current.next = list2; + list2 = list2.next; + } + // move the current pointer to the newly added node + current = current.next; + } + current.next = list1 !== null ? list1 : list2; + return placeholder.next; +}; + +// Time Complexity: O(n+m); +// Space Complexity: O(1) diff --git a/missing-number/yoonthecoder.js b/missing-number/yoonthecoder.js new file mode 100644 index 000000000..6ce3190ce --- /dev/null +++ b/missing-number/yoonthecoder.js @@ -0,0 +1,14 @@ +var missingNumber = function (nums) { + // store all the elemenst from nums in a Set + const set = new Set(nums); + + // check the missing number by iterating through the index + for (i = 0; i <= nums.length; i++) { + if (!set.has(i)) { + return i; + } + } +}; + +// Time complexity: O(n); +// Space complexity: O(n); diff --git a/two-sum/yoonthecoder.js b/two-sum/yoonthecoder.js new file mode 100644 index 000000000..f37002549 --- /dev/null +++ b/two-sum/yoonthecoder.js @@ -0,0 +1,14 @@ +var twoSum = function (nums, target) { + const map = new Map(); + + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i]; + if (map.has(complement)) { + return [map.get(complement), i]; + } + map.set(nums[i], i); + } +}; + +// Time complexity: O(n) +// Space complexity: O(n) - hash map storage