20241024 finished.

This commit is contained in:
2024-10-24 09:08:13 +08:00
parent 3102da99a8
commit bce8de1c85
266 changed files with 2321 additions and 2014 deletions

View File

@@ -9,27 +9,27 @@
* Input: l1 = [2,4,3], l2 = [5,6,4]
* Output: [7,0,8]
* Explanation: 342 + 465 = 807.
*
*
* <strong class="example">Example 2:
*
*
* Input: l1 = [0], l2 = [0]
* Output: [0]
*
*
* <strong class="example">Example 3:
*
*
* Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
* Output: [8,9,9,9,0,0,0,1]
*
*
*
* Constraints:
*
*
* The number of nodes in each linked list is in the range [1, 100].
* 0 <= Node.val <= 9
* It is guaranteed that the list represents a number that does not have leading zeros.
*
*
*/
pub struct Solution {}
use crate::util::linked_list::{ListNode, to_list};
use crate::util::linked_list::{to_list, ListNode};
// problem: https://leetcode.cn/problems/add-two-numbers/
// discuss: https://leetcode.cn/problems/add-two-numbers/discuss/?currentPage=1&orderBy=most_votes&query=
@@ -53,8 +53,10 @@ use crate::util::linked_list::{ListNode, to_list};
// }
// }
impl Solution {
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>)
-> Option<Box<ListNode>> {
pub fn add_two_numbers(
l1: Option<Box<ListNode>>,
l2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
let (mut left, mut right) = (l1, l2);
let mut dummy_head = Some(Box::new(ListNode::new(0)));
let mut now = &mut dummy_head;
@@ -62,13 +64,11 @@ impl Solution {
loop {
if left.is_none() && right.is_none() && overflow == 0 {
break
break;
}
let left_value = match left {
None => {
0
}
None => 0,
Some(node) => {
left = node.next;
node.val
@@ -76,9 +76,7 @@ impl Solution {
};
let right_value = match right {
None => {
0
}
None => 0,
Some(node) => {
right = node.next;
node.val