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; } }
ListNode GetLast(ListNode l) { var iterator = l; while (iterator.next != null) iterator = iterator.next; return iterator; }
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { ListNode result = RecusiveAddTwoNumbers(l1, l2); return result; }