|
| 1 | +package linkedlist; |
| 2 | + |
| 3 | +public class AddTwoIntegers { |
| 4 | + |
| 5 | + |
| 6 | + /* |
| 7 | + * Given head pointers of two linked lists where each linked list represents an integer number |
| 8 | + * (each node is a digit), add them and return the resulting linked list. |
| 9 | + * Input: List1: 1,2,3 List: 1,2 |
| 10 | + * Output: 2, 4, 3 |
| 11 | + * |
| 12 | + * Runtime Complexity: |
| 13 | + * Linear, O(n) |
| 14 | + * |
| 15 | + * Memory Complexity: |
| 16 | + * Constant, O(1) |
| 17 | + * |
| 18 | + * Add the integers 9901 and 237. The result of this addition would be 10138. |
| 19 | + * */ |
| 20 | + |
| 21 | + static LinkedList.Node addIntegers(LinkedList.Node integer1, |
| 22 | + LinkedList.Node integer2) { |
| 23 | + |
| 24 | + LinkedList.Node result = null; |
| 25 | + LinkedList.Node last = null; |
| 26 | + int carry = 0; |
| 27 | + |
| 28 | + while ( |
| 29 | + integer1 != null || |
| 30 | + integer2 != null || |
| 31 | + carry > 0) { |
| 32 | + |
| 33 | + int first = (integer1 == null ? 0 : integer1.data); |
| 34 | + int second = (integer2 == null ? 0 : integer2.data); |
| 35 | + int sum = first + second + carry; |
| 36 | + LinkedList.Node pNew = new LinkedList.Node(sum % 10); |
| 37 | + carry = sum / 10; |
| 38 | + if (result == null) { |
| 39 | + result = pNew; |
| 40 | + } else { |
| 41 | + last.next = pNew; |
| 42 | + } |
| 43 | + |
| 44 | + last = pNew; |
| 45 | + if (integer1 != null) { |
| 46 | + integer1 = integer1.next; |
| 47 | + } |
| 48 | + if (integer2 != null) { |
| 49 | + integer2 = integer2.next; |
| 50 | + } |
| 51 | + } |
| 52 | + return result; |
| 53 | + } |
| 54 | + |
| 55 | + protected static class LinkedList { |
| 56 | + |
| 57 | + private Node head; |
| 58 | + |
| 59 | + public Node head() { |
| 60 | + return head; |
| 61 | + } |
| 62 | + |
| 63 | + public void printNode(Node node) { |
| 64 | + while (node != null) { |
| 65 | + System.out.print(node.data + " "); |
| 66 | + node = node.next; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /* Inserts a new Node at end of the list. */ |
| 71 | + public void push(int new_data) { |
| 72 | + Node newNode = new Node(new_data); |
| 73 | + if (head == null) { |
| 74 | + head = new Node(new_data); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + newNode.next = null; |
| 79 | + Node last = head; |
| 80 | + while (last.next != null) { |
| 81 | + last = last.next; |
| 82 | + } |
| 83 | + last.next = newNode; |
| 84 | + } |
| 85 | + |
| 86 | + public static class Node { |
| 87 | + private Node next; |
| 88 | + private Integer data; |
| 89 | + |
| 90 | + public Node(Integer data) { |
| 91 | + this.data = data; |
| 92 | + } |
| 93 | + |
| 94 | + public Node next() { |
| 95 | + return next; |
| 96 | + } |
| 97 | + |
| 98 | + public void setNext(Node next) { |
| 99 | + this.next = next; |
| 100 | + } |
| 101 | + |
| 102 | + public Integer data() { |
| 103 | + return data; |
| 104 | + } |
| 105 | + |
| 106 | + public void setData(Integer data) { |
| 107 | + this.data = data; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + public static void main(String[] args) { |
| 114 | + |
| 115 | + LinkedList linkedList1 = new LinkedList(); |
| 116 | + linkedList1.push(1); |
| 117 | + linkedList1.push(2); |
| 118 | + linkedList1.push(3); |
| 119 | + |
| 120 | + |
| 121 | + LinkedList linkedList2 = new LinkedList(); |
| 122 | + linkedList2.push(1); |
| 123 | + linkedList2.push(2); |
| 124 | + |
| 125 | + System.out.println("List 1: "); |
| 126 | + linkedList1.printNode(linkedList1.head); |
| 127 | + System.out.println(); |
| 128 | + |
| 129 | + System.out.println("List 2: "); |
| 130 | + linkedList2.printNode(linkedList2.head); |
| 131 | + System.out.println(); |
| 132 | + |
| 133 | + LinkedList.Node mergedList = addIntegers(linkedList1.head, linkedList2.head); |
| 134 | + linkedList1.printNode(mergedList); |
| 135 | + } |
| 136 | +} |
0 commit comments