Skip to content

Commit e706586

Browse files
committed
solution Merge Two Sorted Lists (#224)
- #224
1 parent da3f48a commit e706586

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

โ€Žmerge-two-sorted-lists/jiji-hoon96.tsโ€Ž

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,25 @@
1111
*/
1212

1313
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
14-
15-
};
14+
// ๋”๋ฏธ ํ—ค๋“œ ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘์ ์œผ๋กœ ์‚ฌ์šฉ
15+
const dummy = new ListNode(0);
16+
let current = dummy;
17+
18+
// ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋” ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง„ ๋…ธ๋“œ๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์—ฐ๊ฒฐ
19+
while (list1 !== null && list2 !== null) {
20+
if (list1.val <= list2.val) {
21+
current.next = list1;
22+
list1 = list1.next;
23+
} else {
24+
current.next = list2;
25+
list2 = list2.next;
26+
}
27+
current = current.next;
28+
}
29+
30+
// ๋‚จ์€ ๋…ธ๋“œ๋“ค์„ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์—ฐ๊ฒฐ
31+
current.next = list1 === null ? list2 : list1;
32+
33+
// ๋”๋ฏธ ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ ์‹ค์ œ ๋ณ‘ํ•ฉ๋œ ๋ฆฌ์ŠคํŠธ์˜ ํ—ค๋“œ
34+
return dummy.next;
35+
}

0 commit comments

Comments
ย (0)