public (int carry, Listnode result) Sum(Listnode l1, Listnode l2) { //var num = l1?l1.val?0; //4-> //846 //268 if (l1.next == null && l2.next == null) { var sum = l1.val + l2.val; var carry = sum / 10; return(carry, new Listnode(sum % 10, null)); } else { var result = Sum(l1.next, l2.next); var sum = l1.val + l2.val + result.carry; var carry = sum / 10; sum = sum % 10; return(carry, new Listnode(sum, result.result)); } }
public Listnode(int val, Listnode node) { this.val = val; this.next = node; }