New tasks

This commit is contained in:
nullptroma
2023-03-05 00:46:41 +03:00
parent 1288b304ae
commit b285770a9c
15 changed files with 1018 additions and 0 deletions

View 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>

View 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;
}
}

View File

@@ -0,0 +1,9 @@
namespace _2._Add_Two_Numbers;
internal class Program
{
static void Main(string[] args)
{
}
}

View 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;
}
}