New tasks
This commit is contained in:
11
2. Add Two Numbers/2. Add Two Numbers.csproj
Normal file
11
2. Add Two Numbers/2. Add Two Numbers.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<RootNamespace>_2._Add_Two_Numbers</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
10
2. Add Two Numbers/ListNode.cs
Normal file
10
2. Add Two Numbers/ListNode.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace _2._Add_Two_Numbers;
|
||||
|
||||
public class ListNode {
|
||||
public int val;
|
||||
public ListNode? next;
|
||||
public ListNode(int val=0, ListNode? next=null) {
|
||||
this.val = val;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
9
2. Add Two Numbers/Program.cs
Normal file
9
2. Add Two Numbers/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _2._Add_Two_Numbers;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
40
2. Add Two Numbers/Solution.cs
Normal file
40
2. Add Two Numbers/Solution.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
namespace _2._Add_Two_Numbers;
|
||||
|
||||
public class Solution {
|
||||
public ListNode AddTwoNumbers(ListNode? l1, ListNode? l2)
|
||||
{
|
||||
ListNode? answer = null;
|
||||
ListNode? answerEnd = answer;
|
||||
int toNext = 0;
|
||||
while (l1!=null || l2!=null)
|
||||
{
|
||||
int sum = 0;
|
||||
if (l1 != null)
|
||||
{
|
||||
sum += l1.val;
|
||||
l1 = l1.next;
|
||||
}
|
||||
if (l2 != null)
|
||||
{
|
||||
sum += l2.val;
|
||||
l2 = l2.next;
|
||||
}
|
||||
|
||||
sum += toNext;
|
||||
toNext = sum / 10;
|
||||
sum %= 10;
|
||||
ListNode item = new ListNode(sum);
|
||||
if (answer == null)
|
||||
answer = answerEnd = item;
|
||||
else
|
||||
{
|
||||
answerEnd.next = item;
|
||||
answerEnd = item;
|
||||
}
|
||||
}
|
||||
|
||||
if (toNext != 0)
|
||||
answerEnd.next = new ListNode(toNext);
|
||||
return answer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user