Skip to content

Commit 0f53291

Browse files
committed
https://leetcode.com/problems/add-two-numbers/
1 parent 7a0bac3 commit 0f53291

File tree

2 files changed

+57
-70
lines changed

2 files changed

+57
-70
lines changed

MediumProblems/AddTwoNumbers.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace LeetCode.MediumProblems;
2+
3+
public class AddTwoNumbersSolution
4+
{
5+
public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
6+
{
7+
var node = DfsTraversal(l1, l2, 0);
8+
return node;
9+
}
10+
11+
private ListNode DfsTraversal(ListNode l1, ListNode l2, int carry)
12+
{
13+
int sum = carry;
14+
if (l1 == null && l2 == null)
15+
{
16+
if (sum != 0)
17+
{
18+
return new ListNode(1);
19+
}
20+
else
21+
{
22+
return null;
23+
}
24+
}
25+
26+
if (l1 != null)
27+
{
28+
sum += l1.val;
29+
}
30+
31+
if (l2 != null)
32+
{
33+
sum += l2.val;
34+
}
35+
36+
var node = new ListNode(sum % 10);
37+
node.next = DfsTraversal(l1?.next, l2?.next, sum / 10);
38+
return node;
39+
}
40+
41+
[Test(Description = "https://leetcode.com/problems/add-two-numbers/")]
42+
[Category("Medium")]
43+
[Category("LeetCode")]
44+
[Category("Add Two Numbers")]
45+
[TestCaseSource(nameof(Input))]
46+
public void Test1((int[] Output, (int[], int[]) Input) item)
47+
{
48+
var response = AddTwoNumbers(item.Input.Item1.ToListNode(), item.Input.Item2.ToListNode());
49+
Assert.That(item.Output, Is.EqualTo(response.ToArray()));
50+
}
51+
52+
public static IEnumerable<(int[] Output, (int[], int[]) Input)> Input =>
53+
new List<(int[] Output, (int[], int[]) Input)>()
54+
{
55+
([7, 0, 8], ([2, 4, 3], [5, 6, 4])),
56+
};
57+
}

MediumProblems/Sum Root to Leaf Numbers.cs

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)