You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
(2 -> 4 -> 3) + (5 -> 6 -> 4)
Output : 7->0->8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
package com.omt.learn.algo; import java.util.Calendar; class ListNode{ int val; ListNode next; public ListNode(int val) { this.val = val; } } public class AddTwoNumbers { public static ListNode addTwoNumbers(ListNode l1, ListNode l2) { int sum = l1.val+l2.val; int remaining = sum > 9?1:0; sum = sum%10; ListNode listNode = new ListNode(sum); ListNode firstNode = listNode; while(l1.next != null || l2.next != null) { sum = 0; if(l1.next != null) { l1 = l1.next; sum = l1.val; } if(l2.next != null) { l2 = l2.next; sum+= l2.val; } sum += remaining; remaining = sum > 9?1:0; sum = sum%10; listNode.next = new ListNode(sum); listNode = listNode.next; } if(remaining > 0) { listNode.next = new ListNode(remaining); } return firstNode; } public static void main(String...strings) { ListNode l1 = createListNode("2->4->3"); ListNode l2 = createListNode("5->6->4"); ListNode sum = addTwoNumbers(l1, l2); System.out.println(""); System.out.print(sum.val); while(sum.next != null) { System.out.print("->"+sum.next.val); sum = sum.next; } } private static ListNode createListNode(String data) { String[] array = data.split("->"); ListNode listNode = null; for(String digit : array) { if(listNode == null) { listNode = new ListNode(Integer.parseInt(digit.trim())); } else { ListNode lastOne = listNode; while(lastOne.next != null) { lastOne = lastOne.next; } lastOne.next = new ListNode(Integer.parseInt(digit.trim())); } } return listNode; } } |