# My C# solution 208 ms

• ``````/**
* public class ListNode {
*     public int val;
*     public ListNode next;
*     public ListNode(int x) { val = x; }
* }
*/
public class Solution {
private static double GetIntValue(ListNode ln, Stack<ListNode> s)
{
// push the first list onto a stack
ListNode p = ln;
int ln_size = 0;
while (p != null)
{
s.Push(p);
p = p.next;
ln_size++;
}

// pop all the elements off and return the integer it corresponds to
double op = 0;
for (int i = 0; i < ln_size; i++)
{
// multiply each digit by it's corresponding power of 10
op += Math.Pow(10, (ln_size - i) - 1) * s.Pop().val;
}

return op;
}

public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
{
if (l1 == null && l2 == null) return null;
if (l1 == null) return l2;
if (l2 == null) return l1;

Stack<ListNode> s = new Stack<ListNode>();

double op1 = GetIntValue(l1, s);
double op2 = GetIntValue(l2, s);

string str = (op1 + op2).ToString();

// this is the general case
if (str.Length > 2)
{
ListNode prev = new ListNode((int) Char.GetNumericValue(str[str.Length - 1]));
ListNode ln = new ListNode((int)Char.GetNumericValue(str[str.Length - 2]));
prev.next = ln;

// keep a pointer to the start of the list
ListNode start = prev;

for (int i = str.Length - 3; i >= 0; i--)
{
prev = ln;
ln = new ListNode((int)Char.GetNumericValue(str[i])); // 3

if (i == 0)
ln.next = null;

prev.next = ln;
}

return start;
}
else if (str.Length == 2)
{
ListNode start = new ListNode((int)Char.GetNumericValue(str[1]));
ListNode end = new ListNode((int)Char.GetNumericValue(str[0]));
start.next = end;

return start;
}
else if (str.Length == 1)
{
return new ListNode((int)Char.GetNumericValue(str[0]));
}

return null;
}
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.