| 6 | public class QuestionA { |
| 7 | |
| 8 | private static LinkedListNode addLists( |
| 9 | LinkedListNode l1, LinkedListNode l2, int carry) { |
| 10 | if (l1 == null && l2 == null && carry == 0) { |
| 11 | return null; |
| 12 | } |
| 13 | |
| 14 | LinkedListNode result = new LinkedListNode(); |
| 15 | int value = carry; |
| 16 | if (l1 != null) { |
| 17 | value += l1.data; |
| 18 | } |
| 19 | if (l2 != null) { |
| 20 | value += l2.data; |
| 21 | } |
| 22 | result.data = value % 10; |
| 23 | if (l1 != null || l2 != null) { |
| 24 | LinkedListNode more = addLists(l1 == null ? null : l1.next, |
| 25 | l2 == null ? null : l2.next, |
| 26 | value >= 10 ? 1 : 0); |
| 27 | result.setNext(more); |
| 28 | } |
| 29 | return result; |
| 30 | } |
| 31 | |
| 32 | public static int linkedListToInt(LinkedListNode node) { |
| 33 | int value = 0; |