Skip to content

Commit 0160870

Browse files
committed
merge-two-sorted-lists 풀이 추가
1 parent 955cce8 commit 0160870

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class ListNode {
2+
val: number
3+
next: ListNode | null
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = (val===undefined ? 0 : val)
6+
this.next = (next===undefined ? null : next)
7+
}
8+
}
9+
10+
11+
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
12+
13+
const dummy = new ListNode();
14+
let tail = dummy;
15+
16+
while (list1 && list2) {
17+
if (list1.val <= list2.val) {
18+
tail.next = list1;
19+
list1 = list1.next;
20+
} else {
21+
tail.next = list2;
22+
list2 = list2.next;
23+
}
24+
tail = tail.next;
25+
}
26+
27+
tail.next = list1 || list2;
28+
29+
return dummy.next;
30+
};

0 commit comments

Comments
 (0)