Last active
January 28, 2021 01:39
-
-
Save jbasinger/b39b41a9b384531b84816cfe2a18d73f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ListNode | |
{ | |
public int val; | |
public ListNode next; | |
public ListNode(int val = 0, ListNode next = null) | |
{ | |
this.val = val; | |
this.next = next; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) | |
{ | |
return SumLists(l1, l2, 0); | |
} | |
public ListNode SumLists(ListNode l1, ListNode l2, int carry) | |
{ | |
if (l1 == null && l2 == null && carry == 0) | |
{ | |
return null; | |
} | |
var val1 = l1 == null ? 0 : l1.val; | |
var val2 = l2 == null ? 0 : l2.val; | |
var sum = val1 + val2 + carry; | |
carry = sum >= 10 ? 1 : 0; | |
if (carry == 1) | |
{ | |
sum -= 10; | |
} | |
return new ListNode(sum, SumLists(l1?.next, l2?.next, carry)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void ShouldBuildListNodeFromIntArray() | |
{ | |
var sut = new AddTwoNumbers_Problem(); | |
var input = new[] {2, 4, 3}; | |
var node = sut.BuildList(input); | |
input.ShouldBe(sut.Output(node)); | |
input = new[] {5, 6, 4}; | |
node = sut.BuildList(input); | |
input.ShouldBe(sut.Output(node)); | |
input = new[] {9, 9, 9}; | |
node = sut.BuildList(input); | |
input.ShouldBe(sut.Output(node)); | |
input = new[] {0, 0, 0, 1}; | |
node = sut.BuildList(input); | |
input.ShouldBe(sut.Output(node)); | |
input = new[] {7, 0, 8}; | |
node = sut.BuildList(input); | |
input.ShouldBe(sut.Output(node)); | |
input = new[] {1}; | |
node = sut.BuildList(input); | |
input.ShouldBe(sut.Output(node)); | |
input = new[] {0}; | |
node = sut.BuildList(input); | |
input.ShouldBe(sut.Output(node)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ListNode BuildList(int[] input,int index=0) | |
{ | |
return index+1 < input.Length ? new ListNode(input[index], BuildList(input, ++index)) : new ListNode(input[index]); | |
} | |
public int[] Output(ListNode n) | |
{ | |
var res = new List<int>(); | |
var node = n; | |
while (node != null) | |
{ | |
res.Add(node.val); | |
node = node.next; | |
} | |
return res.ToArray(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void ShouldAddNumbers() | |
{ | |
var sut = new AddTwoNumbers_Problem(); | |
var l1 = sut.BuildList(new[] {2, 4, 3}); | |
var l2 = sut.BuildList(new[] {5,6,4}); | |
var res = sut.AddTwoNumbers(l1, l2); | |
sut.Output(res).ShouldBe(new[] {7, 0, 8}); | |
l1 = sut.BuildList(new[] {0}); | |
l2 = sut.BuildList(new[] {0}); | |
res = sut.AddTwoNumbers(l1, l2); | |
sut.Output(res).ShouldBe(new[] {0}); | |
l1 = sut.BuildList(new[] {9,9,9}); | |
l2 = sut.BuildList(new[] {9}); | |
res = sut.AddTwoNumbers(l1, l2); | |
sut.Output(res).ShouldBe(new[] {8,0,0,1}); | |
l1 = sut.BuildList(new[] {9,9,9,9,9,9,9}); | |
l2 = sut.BuildList(new[] {9,9,9,9}); | |
res = sut.AddTwoNumbers(l1, l2); | |
sut.Output(res).ShouldBe(new[] {8,9,9,9,0,0,0,1}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment