File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ #![ allow( dead_code, unused, unused_variables, non_snake_case) ]
2
+
3
+ fn main ( ) { }
4
+
5
+ struct Solution ;
6
+
7
+ // Definition for singly-linked list.
8
+ #[ derive( PartialEq , Eq , Clone , Debug ) ]
9
+ pub struct ListNode {
10
+ pub val : i32 ,
11
+ pub next : Option < Box < ListNode > > ,
12
+ }
13
+
14
+ impl ListNode {
15
+ #[ inline]
16
+ fn new ( val : i32 ) -> Self {
17
+ ListNode { next : None , val }
18
+ }
19
+ }
20
+ impl Solution {
21
+ pub fn add_two_numbers (
22
+ l1 : Option < Box < ListNode > > ,
23
+ l2 : Option < Box < ListNode > > ,
24
+ ) -> Option < Box < ListNode > > {
25
+ let mut result = ListNode :: new ( 0 ) ;
26
+ let mut current = & mut result;
27
+ let mut s = 0 ; // 进制
28
+ let ( mut l1, mut l2) = ( l1, l2) ;
29
+
30
+ while l1. is_some ( ) || l2. is_some ( ) {
31
+ let a = if let Some ( mut x) = l1 {
32
+ l1 = x. next . take ( ) ;
33
+ x. val
34
+ } else {
35
+ 0
36
+ } ;
37
+
38
+ let b = if let Some ( mut x) = l2 {
39
+ l2 = x. next . take ( ) ;
40
+ x. val
41
+ } else {
42
+ 0
43
+ } ;
44
+ let s1 = ( a + b + s) / 10 ;
45
+ let v = ( a + b + s) % 10 ;
46
+ s = s1;
47
+ current = current. next . insert ( Box :: new ( ListNode :: new ( v) ) ) ;
48
+ }
49
+
50
+ result. next . take ( )
51
+ }
52
+ }
You can’t perform that action at this time.
0 commit comments