-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSumFromLists.cs
39 lines (32 loc) · 1.09 KB
/
SumFromLists.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using PracticeQuestionsSharp.DataStructures;
namespace PracticeQuestionsSharp.Exercises.Linked_List
{
public static class SumFromLists
{
//Two numbers represented by linked lists where each node contains a single digit.
// The digits are stored in reverse order.
// eg. 7 1 6 + 5 9 2 = 617 + 295 -> 2 1 9 (912).
public static LinkedList<int> SumLists(LinkedList<int> left, LinkedList<int> right)
{
LinkedList<int> result = new LinkedList<int>();
Node<int> nodeL = left.Head;
Node<int> nodeR = right.Head;
int place = 1;
int number = 0;
while (nodeL != null || nodeR != null)
{
number += nodeL?.Data * place ?? 0;
number += nodeR?.Data * place ?? 0;
place *= 10;
nodeL = nodeL?.Next;
nodeR = nodeR?.Next;
}
while (number > 0)
{
result.Add(number % 10);
number /= 10;
}
return result;
}
}
}