-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21.dart
More file actions
32 lines (29 loc) · 734 Bytes
/
21.dart
File metadata and controls
32 lines (29 loc) · 734 Bytes
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 {
int val;
ListNode? next;
ListNode([this.val = 0, this.next]);
}
class Solution {
ListNode? mergeTwoLists(ListNode? list1, ListNode? list2) {
ListNode? curNode, headNode;
if (list1 != null && list1.val < (list2?.val ?? 101)) {
curNode = list1;
list1 = list1.next;
} else if (list2 != null) {
curNode = list2;
list2 = list2.next;
}
headNode = curNode;
while (list1 != null || list2 != null) {
if (list1 != null && list1.val < (list2?.val ?? 101)) {
curNode!.next = list1;
list1 = list1.next;
} else {
curNode!.next = list2;
list2 = list2!.next;
}
curNode = curNode.next;
}
return headNode;
}
}