|
| 1 | +package com.antesh.dsa.linkedlist; |
| 2 | +/** |
| 3 | + * Problem Reference: https://github.com/aniketskulkarni/coding-problems/blob/master/LeetCode/src/com/java7/problem/easy/linkedlist/AddTwoNumbersLinkedList.java |
| 4 | + * You are given two non-empty linked lists representing two non-negative |
| 5 | + * integers. The digits are stored in reverse order, and each of their nodes |
| 6 | + * contains a single digit. Add the two numbers and return the sum as a linked |
| 7 | + * list. |
| 8 | + * You may assume the two numbers do not contain any leading zero, except the |
| 9 | + * number 0 itself. |
| 10 | + * |
| 11 | + * Input: l1 = [2,4,3], l2 = [5,6,4] |
| 12 | + * Output: [7,0,8] |
| 13 | + * Explanation: 342 + 465 = 807. |
| 14 | + * |
| 15 | + * Input: l1 = [0], l2 = [0] |
| 16 | + * Output: [0] |
| 17 | + * |
| 18 | + * Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] |
| 19 | + * Output: [8,9,9,9,0,0,0,1] |
| 20 | + */ |
| 21 | +public class AddTwoNumbersLinkedList { |
| 22 | + |
| 23 | + Node n1; |
| 24 | + Node n2; |
| 25 | + Node result; |
| 26 | + |
| 27 | + class Node { |
| 28 | + int data; |
| 29 | + Node next; |
| 30 | + |
| 31 | + public Node(int data) { |
| 32 | + this.data = data; |
| 33 | + this.next = null; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public String print(Node n) { |
| 38 | + StringBuilder sb = new StringBuilder(); |
| 39 | + while (n1 != null) { |
| 40 | + sb.append(n1.data).append(" --> "); |
| 41 | + } |
| 42 | + return sb.toString(); |
| 43 | + } |
| 44 | + |
| 45 | + public Node addToTail(Node head, int data) { |
| 46 | + if (head == null) { |
| 47 | + head = new Node(data); |
| 48 | + return head; |
| 49 | + } |
| 50 | + |
| 51 | + Node ptr = head; |
| 52 | + while (ptr.next != null) { |
| 53 | + ptr = ptr.next; |
| 54 | + } |
| 55 | + ptr.next = new Node(data); |
| 56 | + |
| 57 | + return head; |
| 58 | + } |
| 59 | + |
| 60 | + public Node addTwoNumbers(Node n1, Node n2) { |
| 61 | + if (n1 == null && n2 == null) return null; |
| 62 | + if (n1 == null) return n2; |
| 63 | + if (n2 == null) return n1; |
| 64 | + |
| 65 | + Node result = null; |
| 66 | + int carry = 0; |
| 67 | + |
| 68 | + while (n1 != null && n2 != null) { |
| 69 | + int sum = carry + n1.data + n2.data; |
| 70 | + if ( sum > 9) { |
| 71 | + carry = 1; |
| 72 | + sum = sum % 10; |
| 73 | + } |
| 74 | + |
| 75 | + result = addToTail(result, sum); |
| 76 | + |
| 77 | + n1 = n1.next; |
| 78 | + n2 = n2.next; |
| 79 | + } |
| 80 | + |
| 81 | + while (n1 != null) { |
| 82 | + addToTail(result, n1.data + carry); |
| 83 | + carry = 0; |
| 84 | + n1 = n1.next; |
| 85 | + } |
| 86 | + |
| 87 | + while (n2 != null) { |
| 88 | + addToTail(result, n2.data + carry); |
| 89 | + carry = 0; |
| 90 | + n2 = n2.next; |
| 91 | + } |
| 92 | + |
| 93 | + if (carry > 0) { |
| 94 | + result = addToTail(result, carry); |
| 95 | + } |
| 96 | + |
| 97 | + return result; |
| 98 | + } |
| 99 | + |
| 100 | + public static void main(String[] args) { |
| 101 | + AddTwoNumbersLinkedList list = new AddTwoNumbersLinkedList(); |
| 102 | + for (int i = 3; i < 6; i++) { |
| 103 | + list.n1 = list.addToTail(list.n1, i); |
| 104 | + list.n2 = list.addToTail(list.n2, i); |
| 105 | + } |
| 106 | + |
| 107 | + list.addTwoNumbers(list.n1, list.n2); |
| 108 | + } |
| 109 | +} |
0 commit comments