File tree Expand file tree Collapse file tree 3 files changed +87
-1
lines changed
C#/LeetCodeResolves/LeetCodeResolves Expand file tree Collapse file tree 3 files changed +87
-1
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Text ;
4
+
5
+ namespace LeetCodeResolves . AddTwoNumbers
6
+ {
7
+ public class ListNode
8
+ {
9
+ public int value ;
10
+ public ListNode next ;
11
+ public ListNode ( int value = 0 , ListNode next = null )
12
+ {
13
+ this . value = value ;
14
+ this . next = next ;
15
+ }
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Text ;
4
+
5
+ namespace LeetCodeResolves . AddTwoNumbers
6
+ {
7
+ class Program
8
+ {
9
+ static void Main ( string [ ] args )
10
+ {
11
+ var fNode = new ListNode
12
+ {
13
+ value = 2 ,
14
+ next = new ListNode
15
+ {
16
+ value = 4 ,
17
+ next = new ListNode
18
+ {
19
+ value = 3 ,
20
+ next = null
21
+ }
22
+ }
23
+ } ;
24
+
25
+ var sNode = new ListNode
26
+ {
27
+ value = 5 ,
28
+ next = new ListNode
29
+ {
30
+ value = 6 ,
31
+ next = new ListNode
32
+ {
33
+ value = 4 ,
34
+ next = null
35
+ }
36
+ }
37
+ } ;
38
+
39
+ var result = AddTwoNumbers ( fNode , sNode ) ;
40
+
41
+ Console . ReadLine ( ) ;
42
+ }
43
+
44
+ static ListNode AddTwoNumbers ( ListNode l1 , ListNode l2 )
45
+ {
46
+ var dummyHead = new ListNode ( 0 ) ;
47
+ ListNode p = l1 , q = l2 , current = dummyHead ;
48
+ var carry = 0 ;
49
+ while ( p != null || q != null )
50
+ {
51
+ var x = p != null ? p . value : 0 ;
52
+ var y = q != null ? q . value : 0 ;
53
+ var sum = carry + x + y ;
54
+ carry = sum / 10 ;
55
+ current . next = new ListNode ( sum % 10 ) ;
56
+ current = current . next ;
57
+ if ( p != null ) p = p . next ;
58
+ if ( q != null ) q = q . next ;
59
+ }
60
+
61
+ if ( carry > 0 )
62
+ {
63
+ current . next = new ListNode ( carry ) ;
64
+ }
65
+
66
+ return dummyHead . next ;
67
+ }
68
+ }
69
+ }
Original file line number Diff line number Diff line change 3
3
<PropertyGroup >
4
4
<OutputType >Exe</OutputType >
5
5
<TargetFramework >netcoreapp3.1</TargetFramework >
6
- <StartupObject >LeetCodeResolves.TwoSum .Program</StartupObject >
6
+ <StartupObject >LeetCodeResolves.AddTwoNumbers .Program</StartupObject >
7
7
</PropertyGroup >
8
8
9
9
</Project >
You can’t perform that action at this time.
0 commit comments