You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { /*if (l1 == null) return l2; if (l2 == null) return l1; ListNode newNode = new ListNode(0); ListNode nextNode = new ListNode(0); newNode.val = (l1.val + l2.val)%10; newNode.next = nextNode; nextNode = AddTwoNumbers( l1.next, l2.next); nextNode.val +=((l1.val + l2.val)/10 ==1)? 1 : 0;*/ //return AddTwoNumbers(l1,l2,0); ListNode result = new ListNode(0); int add = 0; if(l1 == null) return l2; if(l2 == null) return l1; int sum =0; ListNode stand = result; while((l1 != null)||(l2 != null)) { if(l1 != null) { sum += l1.val; l1 = l1.next; } if(l2 != null) { sum += l2.val; l2 = l2.next; } //sum+=add; result.next = new ListNode(sum%10); result = result.next; sum = sum/10; } //judge highest digit add or not result.next = (sum==1)?new ListNode(1):null; return stand.next; }