Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 041b4e0

Browse files
feat(LC): add 21; solutions, links, explaination images
1 parent 387e66a commit 041b4e0

File tree

3 files changed

+213
-0
lines changed

3 files changed

+213
-0
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/**
2+
* @see {@link https://leetcode.com/problems/merge-two-sorted-lists/?envType=study-plan&id=level-1 21. Merge Two Sorted Lists}
3+
*/
4+
5+
/**
6+
* EXPLAIN
7+
*
8+
* given two LL where vals of each node are guaranteed to be the lowest in each LL
9+
* need to return the HEAD of each list
10+
* not guaranteed to be same list size
11+
*/
12+
13+
/**
14+
* APPROACH
15+
*
16+
* create new LL
17+
* create separate ref value to new LL
18+
* create refs to L1 and L2
19+
*
20+
* WHILE LOOP until both LL dont exist
21+
*
22+
* IF L1 DNE but L2 exists
23+
* create next on new LL using L2
24+
* advance ref on new LL
25+
* continue
26+
* IF L2 but DNE but L1 exists
27+
* create next of new LL using L1
28+
* advance ref on new LL
29+
* continue
30+
*
31+
* IF L1 val greater than L2
32+
* create next on new LL using L1 val
33+
* advance ref to L1 next
34+
* advance ref on new LL
35+
* ELSE IF L2 val greater than L1
36+
* create next on new LL using L2 val
37+
* advance ref to L2 next
38+
* advance ref on new LL
39+
* ELSE both L1 and L2 are the same
40+
* create next on new LL using L1
41+
* create next.next on new LL using L2
42+
* advance both L1 and L2 to next
43+
* advance new LL twice
44+
* LOOP ENDS when L1 and L2 DNE
45+
*
46+
* return separate ref val to new LL
47+
*/
48+
49+
/**
50+
* CODE [FAIL]
51+
* issue where I would get leading 0 value for undefined vals when handling empty lists
52+
*/
53+
/**
54+
* Definition for singly-linked list.
55+
* function ListNode(val, next) {
56+
* this.val = (val===undefined ? 0 : val)
57+
* this.next = (next===undefined ? null : next)
58+
* }
59+
*/
60+
/**
61+
* @param {ListNode} list1
62+
* @param {ListNode} list2
63+
* @return {ListNode}
64+
*/
65+
var mergeTwoLists = function (list1, list2) {
66+
const merged = new ListNode();
67+
let mergedRef = merged;
68+
69+
// BASE CASE: NO NODES
70+
if (!list1 && !list2) return new ListNode();
71+
72+
while (list1 || list2) {
73+
// CASES: one of the lists in null, other still has vals
74+
if (!list1) {
75+
mergedRef.next = new ListNode(list2.val);
76+
mergedRef = mergedRef.next;
77+
list2 = list2.next;
78+
continue;
79+
}
80+
81+
if (!list2) {
82+
mergedRef.next = new ListNode(list1.val);
83+
mergedRef = mergedRef.next;
84+
list1 = list1.next;
85+
continue;
86+
}
87+
88+
// CASES: both lists still have vals
89+
if (list1.val > list2.val) {
90+
mergedRef.next = new ListNode(list1.val);
91+
mergedRef = mergedRef.next;
92+
list1 = list1.next;
93+
} else if (list2.val > list1.val) {
94+
mergedRef.next = new ListNode(list2.val);
95+
mergedRef.next = mergedRef;
96+
list2 = list2.next;
97+
} else {
98+
mergedRef.next = new ListNode(list1.val);
99+
mergedRef.next.next = new ListNode(list2.val);
100+
mergedRef = mergedRef.next.next;
101+
list1 = list1.next;
102+
list2 = list2.next;
103+
}
104+
}
105+
return merged;
106+
};
107+
108+
/**
109+
* Compare with working solution
110+
*/
111+
/**
112+
* Definition for singly-linked list.
113+
* function ListNode(val, next) {
114+
* this.val = (val===undefined ? 0 : val)
115+
* this.next = (next===undefined ? null : next)
116+
* }
117+
*/
118+
/**
119+
* @param {ListNode} list1
120+
* @param {ListNode} list2
121+
* @return {ListNode}
122+
*/
123+
var mergeTwoLists = function (list1, list2) {
124+
let head = new ListNode(),
125+
tail;
126+
127+
if (!list1 || !list2) {
128+
return list2 || list1;
129+
}
130+
131+
if (list1.val < list2.val) {
132+
head = list1;
133+
list1 = list1.next;
134+
} else {
135+
head = list2;
136+
list2 = list2.next;
137+
}
138+
tail = head;
139+
140+
while (list1 && list2) {
141+
if (list1.val < list2.val) {
142+
tail.next = list1;
143+
tail = tail.next;
144+
list1 = list1.next;
145+
} else if (list1.val > list2.val) {
146+
tail.next = list2;
147+
tail = tail.next;
148+
list2 = list2.next;
149+
} else if (list1.val == list2.val) {
150+
tail.next = list1;
151+
tail = tail.next;
152+
list1 = list1.next;
153+
tail.next = list2;
154+
tail = tail.next;
155+
list2 = list2.next;
156+
}
157+
}
158+
tail.next = list1 || list2;
159+
return head;
160+
};
161+
162+
/**
163+
* SOLUTION USED
164+
* Recursive solution
165+
* https://leetcode.com/problems/merge-two-sorted-lists/solutions/2705782/js-recursion-with-exlanation/?envType=study-plan&id=level-1&orderBy=most_votes&languageTags=javascript
166+
*/
167+
168+
var mergeTwoLists = function (l1, l2) {
169+
if (!l1) return l2;
170+
else if (!l2) return l1;
171+
else if (l1.val <= l2.val) {
172+
l1.next = mergeTwoLists(l1.next, l2);
173+
return l1;
174+
} else {
175+
l2.next = mergeTwoLists(l1, l2.next);
176+
return l2;
177+
}
178+
};
179+
180+
/**
181+
* WHILE LOOP Solution
182+
* https://leetcode.com/problems/merge-two-sorted-lists/solutions/2630303/fast-typescript-solution-75-ms-top-95-speed/
183+
*/
184+
function mergeTwoLists(
185+
list1: ListNode | null,
186+
list2: ListNode | null
187+
): ListNode | null {
188+
const mergedHead: ListNode = new ListNode(-1, null);
189+
let prev: ListNode = mergedHead;
190+
191+
while (list1 && list2) {
192+
if (list1.val <= list2.val) {
193+
prev.next = list1;
194+
list1 = list1.next;
195+
} else {
196+
prev.next = list2;
197+
list2 = list2.next;
198+
}
199+
prev = prev.next;
200+
}
201+
prev.next = list1 ? list1 : list2;
202+
203+
return mergedHead.next;
204+
}
205+
206+
/**
207+
* READ 09:08
208+
* EXPLAIN 06:51 15:59
209+
* APPROACH 05:08 21:08
210+
* CODE 09:01 30:09
211+
* TEST [FAIL] 09:44 39:53
212+
* OPTIMIZE
213+
*/
183 KB
Loading[フレーム]
167 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /