-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.dart
More file actions
32 lines (26 loc) · 646 Bytes
/
2.dart
File metadata and controls
32 lines (26 loc) · 646 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 Solution {
ListNode? addTwoNumbers(ListNode? l1, ListNode? l2) {
if (l1 == null && l2 == null) {
return null;
}
int value = (l1?.val ?? 0) + (l2?.val ?? 0);
int remainder = value ~/ 10;
value %= 10;
final remainNode = l1?.next ?? l2?.next;
final node = new ListNode(value);
if (remainNode == null) {
if (remainder != 0) {
node.next = ListNode(remainder);
}
} else {
remainNode.val += remainder;
node.next = addTwoNumbers(l1?.next, l2?.next);
}
return node;
}
}
class ListNode {
int val;
ListNode? next;
ListNode([this.val = 0, this.next]);
}