Exemplo n.º 1
0
 private LNode<Integer> addRecursive(LNode<Integer> n1, LNode<Integer> n2, int carry)
     throws Exception {
   if ((n1 != null && (n1.data < 0 || n1.data > 9))
       || (n2 != null && (n2.data < 0 || n2.data > 9))) throw new Exception("Bad List.");
   if (n1 == null && n2 == null && carry == 0) {
     return null;
   } else {
     int sum = (n1 != null ? n1.data : 0) + (n2 != null ? n2.data : 0) + carry;
     LNode<Integer> ret = new LNode<Integer>(sum % 10);
     ret.next = addRecursive(n1 != null ? n1.next : null, n2 != null ? n2.next : null, sum / 10);
     return ret;
   }
 }