Skip to content

Commit abf33f3

Browse files
committed
feat: add Merge Two Sorted Lists
1 parent ce30a3a commit abf33f3

File tree

3 files changed

+84
-44
lines changed

3 files changed

+84
-44
lines changed

README.md

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,43 @@ https://neetcode.io/roadmap
44

55
### NeetCode
66

7-
| # | Title | Difficulty | Solution | Category |
8-
| --- | ------------------------------------------------------------------------------------------------------------------------------- | ---------- | ---------------------------------------------------------------------------------------------------- | ---------------- |
9-
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | Easy | [ts](./TypeScript/217.contains-duplicate.ts) | Arrays & Hashing |
10-
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | Easy | [ts](./TypeScript/242.valid-anagram.ts) | Arrays & Hashing |
11-
| 242 | [Two Sum](https://leetcode.com/problems/two-sum/) | Easy | [ts](./TypeScript/1.two-sum.ts) | Arrays & Hashing |
12-
| 49 | [Group Anagrams](https://leetcode.com/problems/two-sum/) | Medium | [ts](./TypeScript/49.group-anagrams.ts) | Arrays & Hashing |
13-
| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | Medium | [ts](./TypeScript/347.top-k-frequent-elements.ts) | Arrays & Hashing |
14-
| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | Medium | [ts](./TypeScript/238.product-of-array-except-self.ts) | Arrays & Hashing |
15-
| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | Medium | [ts](./TypeScript/36.valid-sudoku.ts) | Arrays & Hashing |
16-
| 659 | [Encode and Decode Strings](https://www.lintcode.com/problem/659/) | Medium | [ts](./TypeScript/659.encode-and-decode-strings.ts) | Arrays & Hashing |
17-
| 128 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/) | Medium | [ts](./TypeScript/128.longest-consecutive-sequence.ts) | Arrays & Hashing |
18-
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | Easy | [ts](./TypeScript/125.valid-palindrome.ts) | Two Pointers |
19-
| 167 | [Two Sum II - Input Array Is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | Medium | [ts](./TypeScript/167.two-sum-ii-input-array-is-sorted.ts) | Two Pointers |
20-
| 15 | [3Sum](https://leetcode.com/problems/3sum/) | Medium | [ts](./TypeScript/15.3sum.ts) | Two Pointers |
21-
| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | Medium | [ts](./TypeScript/11.container-with-most-water.ts) | Two Pointers |
22-
| 42 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) | Hard | [ts](./TypeScript/42.trapping-rain-water.ts) | Two Pointers |
23-
| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | Easy | [ts](./TypeScript/20.valid-parentheses.ts) | Stack |
24-
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | Medium | [ts](./TypeScript/155.min-stack.ts) | Stack |
25-
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | Medium | [ts](./TypeScript/150.evaluate-reverse-polish-notation.ts) | Stack |
26-
| 22 | [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) | Medium | [ts](./TypeScript/22.generate-parentheses.ts) | Stack |
27-
| 739 | [Daily Temperatures](https://leetcode.com/problems/daily-temperatures/) | Medium | [ts](./TypeScript/739.daily-temperatures.ts) | Stack |
28-
| 853 | [Car Fleet](https://leetcode.com/problems/car-fleet/) | Medium | [ts](./TypeScript/853.car-fleet.ts) | Stack |
29-
| 84 | [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) | Hard | [ts](./TypeScript/84.largest-rectangle-in-histogram.ts) | Stack |
30-
| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | Easy | [ts](./TypeScript/704.binary-search.ts) | Binary Search |
31-
| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | Medium | [ts](./TypeScript/74.search-a-2d-matrix.ts) | Binary Search |
32-
| 875 | [Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/) | Medium | [ts](./TypeScript/875.koko-eating-bananas.ts) | Binary Search |
33-
| 153 | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | Medium | [ts](./TypeScript/153.find-minimum-in-rotated-sorted-array.ts) | Binary Search |
34-
| 33 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | Medium | [ts](./TypeScript/33.search-in-rotated-sorted-array.ts) | Binary Search |
35-
| 981 | [Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | Medium | [ts](./TypeScript/981.time-based-key-value-store.ts) | Binary Search |
36-
| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | Easy | [ts](./TypeScript/121.best-time-to-buy-and-sell-stock.ts) | Sliding Window |
37-
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | Medium | [ts](./TypeScript/3.longest-substring-without-repeating-characters.ts) | Sliding Window |
38-
| 424 | [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/) | Medium | [ts](./TypeScript/424.longest-repeating-character-replacement.ts) | Sliding Window |
7+
| # | Title | Difficulty | Solution | Category |
8+
| --- | ------------------------------------------------------------------------------------------------------------------------------- | ---------- | ----------------------------------------------------------------------------------------- | ---------------- |
9+
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | Easy | [ts](./TypeScript/217.contains-duplicate.ts) | Arrays & Hashing |
10+
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | Easy | [ts](./TypeScript/242.valid-anagram.ts) | Arrays & Hashing |
11+
| 242 | [Two Sum](https://leetcode.com/problems/two-sum/) | Easy | [ts](./TypeScript/1.two-sum.ts) | Arrays & Hashing |
12+
| 49 | [Group Anagrams](https://leetcode.com/problems/two-sum/) | Medium | [ts](./TypeScript/49.group-anagrams.ts) | Arrays & Hashing |
13+
| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | Medium | [ts](./TypeScript/347.top-k-frequent-elements.ts) | Arrays & Hashing |
14+
| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | Medium | [ts](./TypeScript/238.product-of-array-except-self.ts) | Arrays & Hashing |
15+
| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | Medium | [ts](./TypeScript/36.valid-sudoku.ts) | Arrays & Hashing |
16+
| 659 | [Encode and Decode Strings](https://www.lintcode.com/problem/659/) | Medium | [ts](./TypeScript/659.encode-and-decode-strings.ts) | Arrays & Hashing |
17+
| 128 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/) | Medium | [ts](./TypeScript/128.longest-consecutive-sequence.ts) | Arrays & Hashing |
18+
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | Easy | [ts](./TypeScript/125.valid-palindrome.ts) | Two Pointers |
19+
| 167 | [Two Sum II - Input Array Is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | Medium | [ts](./TypeScript/167.two-sum-ii-input-array-is-sorted.ts) | Two Pointers |
20+
| 15 | [3Sum](https://leetcode.com/problems/3sum/) | Medium | [ts](./TypeScript/15.3sum.ts) | Two Pointers |
21+
| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | Medium | [ts](./TypeScript/11.container-with-most-water.ts) | Two Pointers |
22+
| 42 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) | Hard | [ts](./TypeScript/42.trapping-rain-water.ts) | Two Pointers |
23+
| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | Easy | [ts](./TypeScript/20.valid-parentheses.ts) | Stack |
24+
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | Medium | [ts](./TypeScript/155.min-stack.ts) | Stack |
25+
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | Medium | [ts](./TypeScript/150.evaluate-reverse-polish-notation.ts) | Stack |
26+
| 22 | [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) | Medium | [ts](./TypeScript/22.generate-parentheses.ts) | Stack |
27+
| 739 | [Daily Temperatures](https://leetcode.com/problems/daily-temperatures/) | Medium | [ts](./TypeScript/739.daily-temperatures.ts) | Stack |
28+
| 853 | [Car Fleet](https://leetcode.com/problems/car-fleet/) | Medium | [ts](./TypeScript/853.car-fleet.ts) | Stack |
29+
| 84 | [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) | Hard | [ts](./TypeScript/84.largest-rectangle-in-histogram.ts) | Stack |
30+
| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | Easy | [ts](./TypeScript/704.binary-search.ts) | Binary Search |
31+
| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | Medium | [ts](./TypeScript/74.search-a-2d-matrix.ts) | Binary Search |
32+
| 875 | [Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/) | Medium | [ts](./TypeScript/875.koko-eating-bananas.ts) | Binary Search |
33+
| 153 | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | Medium | [ts](./TypeScript/153.find-minimum-in-rotated-sorted-array.ts) | Binary Search |
34+
| 33 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | Medium | [ts](./TypeScript/33.search-in-rotated-sorted-array.ts) | Binary Search |
35+
| 981 | [Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | Medium | [ts](./TypeScript/981.time-based-key-value-store.ts) | Binary Search |
36+
| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | Easy | [ts](./TypeScript/121.best-time-to-buy-and-sell-stock.ts) | Sliding Window |
37+
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | Medium | [ts](./TypeScript/3.longest-substring-without-repeating-characters.ts) | Sliding Window |
38+
| 424 | [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/) | Medium | [ts](./TypeScript/424.longest-repeating-character-replacement.ts) | Sliding Window |
3939
| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | Easy | [ts](./TypeScript/206.reverse-linked-list.ts) [rkt](./Racket/206.reverse-linked-list.rkt) | Linked List |
40+
| 21 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | Easy | [ts](./TypeScript/21.merge-two-sorted-lists.ts) | Linked List |
4041

4142
### Others
4243

43-
| # | Title | Difficulty | Solution | Category |
44-
| ---- | ------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | ------------------------------------------------------------------------------------------ | ------------ |
44+
| # | Title | Difficulty | Solution | Category |
45+
| ---- | ------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | ---------------------------------------------------------------------------------- | ------------ |
4546
| 2414 | [Length of the Longest Alphabetical Continuous Substring](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/) | Medium | [ts](./TypeScript/2414.length-of-the-longest-alphabetical-continuous-substring.ts) | Two Pointers |

TypeScript/206.reverse-linked-list.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
// (define (rev l)
2-
// (if (null? (cdr l))
3-
// l
4-
// (append (rev (cdr l)) (list (car l)))))
5-
6-
class ListNode {
1+
interface ListNode {
72
val: number;
83
next: ListNode | null;
9-
constructor(val?: number, next?: ListNode | null) {
10-
this.val = val === undefined ? 0 : val;
11-
this.next = next === undefined ? null : next;
12-
}
134
}
145

156
function reverseList(head: ListNode | null): ListNode | null {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
interface ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
}
5+
6+
function mergeTwoLists(
7+
list1: ListNode | null,
8+
list2: ListNode | null
9+
): ListNode | null {
10+
if (list1 === null) {
11+
return list2;
12+
} else if (list2 === null) {
13+
return list1;
14+
} else if (list1.val <= list2.val) {
15+
list1.next = mergeTwoLists(list1.next, list2);
16+
return list1;
17+
} else {
18+
list2.next = mergeTwoLists(list1, list2.next);
19+
return list2;
20+
}
21+
}
22+
23+
function mergeTwoLists2(
24+
list1: ListNode | null,
25+
list2: ListNode | null
26+
): ListNode | null {
27+
let dummyList: ListNode = { val: -1, next: null }; // create a reference and hold it
28+
let current: ListNode = dummyList;
29+
30+
while (list1 && list2) {
31+
if (list1.val <= list2.val) {
32+
current.next = list1;
33+
list1 = list1.next;
34+
} else {
35+
current.next = list2;
36+
list2 = list2.next;
37+
}
38+
current = current.next;
39+
}
40+
if (list1 === null) {
41+
current.next = list2;
42+
}
43+
if (list2 === null) {
44+
current.next = list1;
45+
}
46+
47+
return dummyList.next;
48+
}

0 commit comments

Comments
 (0)