Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
52 changes: 52 additions & 0 deletions merge-two-sorted-lists/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* 21. Merge Two Sorted Lists
* You are given the heads of two sorted linked lists list1 and list2.
* Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
* Return the head of the merged linked list.
*
* https://leetcode.com/problems/merge-two-sorted-lists/description/
*/

/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

// O(n + m) time
// O(1) space
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

재귀 호출인 만큼 스택 공간이 필요할것으로 보여요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list1과 list2이 있으니 O(n + m)이 되겠군요...!

function mergeTwoLists(
list1: ListNode | null,
list2: ListNode | null
): ListNode | null {
if (!list1) return list2;
if (!list2) return list1;

let mergedListNode: ListNode;
const val1 = list1.val;
const val2 = list2.val;
if (val1 > val2) {
mergedListNode = new ListNode(val2);
mergedListNode.next = mergeTwoLists(list1, list2.next);
} else {
mergedListNode = new ListNode(val1);
mergedListNode.next = mergeTwoLists(list1.next, list2);
}

return mergedListNode;
}

class ListNode {
val: number;
next: ListNode | null;
constructor(val?: number, next?: ListNode | null) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
}
20 changes: 20 additions & 0 deletions missing-number/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 268. Missing Number
* Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
*
* https://leetcode.com/problems/missing-number/description/
*/

// O(n) time
// O(1) space
function missingNumber(nums: number[]): number {
const n = nums.length;
let total = (n * (n + 1)) / 2;

let sum = 0;
for (let i = 0; i < n; i++) {
sum += nums[i];
}

return total - sum;
}
Loading