445. Add Two Numbers II Medium

@problem@discussion
#Linked List#Math#Stack



1/**
2 * [445] Add Two Numbers II
3 *
4 * You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
5 * You may assume the two numbers do not contain any leading zero, except the number 0 itself.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/sumii-linked-list.jpg" style="width: 523px; height: 342px;" />
9 * Input: l1 = [7,2,4,3], l2 = [5,6,4]
10 * Output: [7,8,0,7]
11 * 
12 * Example 2:
13 * 
14 * Input: l1 = [2,4,3], l2 = [5,6,4]
15 * Output: [8,0,7]
16 * 
17 * Example 3:
18 * 
19 * Input: l1 = [0], l2 = [0]
20 * Output: [0]
21 * 
22 *  
23 * Constraints:
24 * 
25 * 	The number of nodes in each linked list is in the range [1, 100].
26 * 	0 <= Node.val <= 9
27 * 	It is guaranteed that the list represents a number that does not have leading zeros.
28 * 
29 *  
30 * Follow up: Could you solve it without reversing the input lists?
31 * 
32 */
33pub struct Solution {}
34use crate::util::linked_list::{ListNode, to_list};
35
36// problem: https://leetcode.com/problems/add-two-numbers-ii/
37// discuss: https://leetcode.com/problems/add-two-numbers-ii/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41// Definition for singly-linked list.
42// #[derive(PartialEq, Eq, Clone, Debug)]
43// pub struct ListNode {
44//   pub val: i32,
45//   pub next: Option<Box<ListNode>>
46// }
47// 
48// impl ListNode {
49//   #[inline]
50//   fn new(val: i32) -> Self {
51//     ListNode {
52//       next: None,
53//       val
54//     }
55//   }
56// }
57impl Solution {
58    pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
59        Some(Box::new(ListNode::new(0)))
60    }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_445() {
71    }
72}
73


Back
© 2025 bowen.ge All Rights Reserved.