Skip to content

Commit 184be39

Browse files
committed
풀이1: Merge Two Sorted Lists #224
1 parent 3fc20a0 commit 184be39

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 시간 복잡도: O(n + m)
3+
* 공간 복잡도: O(1)
4+
*/
5+
var mergeTwoLists = function (list1, list2) {
6+
const dummy = new ListNode();
7+
node = dummy;
8+
9+
while (list1 && list2) {
10+
if (list1.val < list2.val) {
11+
node.next = list1;
12+
list1 = list1.next;
13+
} else {
14+
node.next = list2;
15+
list2 = list2.next;
16+
}
17+
node = node.next;
18+
}
19+
20+
node.next = list1 || list2;
21+
22+
return dummy.next;
23+
};

0 commit comments

Comments
 (0)