-
Notifications
You must be signed in to change notification settings - Fork 42
/
solution.ts
32 lines (31 loc) · 859 Bytes
/
solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class ListNode {
val: number
next: ListNode | null
constructor (val?: number, next?: ListNode | null) {
this.val = (val === undefined ? 0 : val);
this.next = (next === undefined ? null : next);
}
}
function mergeTwoLists (l1: ListNode | null, l2: ListNode | null): ListNode | null {
const dummyHead:ListNode = new ListNode(0);
let last = dummyHead;
while (l1 !== null && l2 !== null) {
if (l1.val < l2.val) {
last.next = l1;
last = last.next;
l1 = l1.next;
last.next = null;
} else {
last.next = l2;
last = last.next;
l2 = l2.next;
last.next = null;
}
}
if (l1 !== null) {
last.next = l1;
} else if (l2 !== null) {
last.next = l2;
}
return dummyHead.next;
}