Skip to content

Commit 07e0929

Browse files
committed
feat: 문제풀이 추가
1 parent 5b1c79d commit 07e0929

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

merge-two-sorted-lists/hwanmini.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 시간복잡도: O(m + n)
2+
// 공간복잡도: O(m + n)
3+
4+
/**
5+
* Definition for singly-linked list.
6+
* function ListNode(val, next) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
*/
11+
/**
12+
* @param {ListNode} list1
13+
* @param {ListNode} list2
14+
* @return {ListNode}
15+
*/
16+
var mergeTwoLists = function(list1, list2) {
17+
let res = new ListNode()
18+
let resCopy = res
19+
20+
while (list1 && list2) {
21+
if (list1.val < list2.val) {
22+
res.next = new ListNode(list1.val);
23+
list1 = list1.next;
24+
} else {
25+
res.next = new ListNode(list2.val);
26+
list2 = list2.next;
27+
}
28+
29+
res = res.next
30+
}
31+
32+
if (list1) res.next = list1;
33+
if (list2) res.next = list2
34+
35+
return resCopy.next
36+
};
37+

0 commit comments

Comments
 (0)