Perform Addition
Consider the length of two linked list.
while(ll1.Count != ll2.Count)
{
if (ll1.Count < ll2.Count)
{
ll1.AddFirst(0);
}
else
ll2.AddFirst(0);
}
Main class.
LinkedList<int> ll1 = new LinkedList<int>();
ll1.AddLast(7);
ll1.AddLast(5);
ll1.AddLast(9);
ll1.AddLast(4);
ll1.AddLast(6);
LinkedList<int> ll2 = new LinkedList<int>();
ll2.AddLast(8);
ll2.AddLast(4);
LinkedList<int> ll3 = new LinkedList<int>();
Last, for loop method.
int temp = 0, carry = 0;
Console.WriteLine(ll1.ElementAtOrDefault(4));
for (int i = ll2.Count-1; i>=0; i--)
{
temp = ll1.Last() + ll2.Last();
ll3.AddFirst((temp + carry) % 10);
carry = (temp + carry) / 10;
ll1.RemoveLast();
ll2.RemoveLast();
}