예제 #1
0
 ListNode RecusiveAddTwoNumbers(ListNode l1, ListNode l2)
 {
     if (l1.next == null && l2.next == null)
     {
         var result = new ListNode(l1.val + l2.val);
         return result;
     }
     else
     {
         var current = new ListNode(l1.val + l2.val);
         var previous = RecusiveAddTwoNumbers(l1.next, l2.next);
         var last = GetLast(previous);
         if (last.val >= 10)
         {
             current.val += last.val / 10;
             last.val = last.val % 10;
         }
         last.next = current;
         return previous;
     }
 }
예제 #2
0
 ListNode GetLast(ListNode l)
 {
     var iterator = l;
     while (iterator.next != null) iterator = iterator.next;
     return iterator;
 }
예제 #3
0
 public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
 {
     ListNode result = RecusiveAddTwoNumbers(l1, l2);
     return result;
 }