Skip to content

Commit 9ea2b26

Browse files
committed
add: mergeTwoSortedLists solution
1 parent 91c1722 commit 9ea2b26

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} list1
10+
* @param {ListNode} list2
11+
* @return {ListNode}
12+
*/
13+
//! ์ถ”ํ›„ ๋ณต์Šต ํ•„์š”ํ•œ ๋ฌธ์ œ
14+
const mergeTwoLists = function (list1, list2) {
15+
// ์ด ๋ถ€๋ถ„์„ ์ œํ•œ ์‹œ๊ฐ„ ๋‚ด์— ๋– ์˜ฌ๋ฆฌ์ง€ ๋ชปํ•ด ai๋กœ๋ถ€ํ„ฐ ํžŒํŠธ๋ฅผ ๋ฐ›์•„ ๋กœ์ง์„ ์ž‘์„ฑ
16+
let temp = new ListNode(0);
17+
let cur = temp;
18+
19+
while (list1 !== null && list2 !== null) {
20+
if (list1.val <= list2.val) {
21+
cur.next = list1;
22+
list1 = list1.next;
23+
} else {
24+
cur.next = list2;
25+
list2 = list2.next;
26+
}
27+
cur = cur.next;
28+
}
29+
30+
cur.next = list1 !== null ? list1 : list2;
31+
32+
return temp.next;
33+
};

0 commit comments

Comments
ย (0)