|
| 1 | +/** |
| 2 | + * MultiplyTwoNumbersLL |
| 3 | + * |
| 4 | + * Description: |
| 5 | + * This class multiplies two numbers represented as singly linked lists. |
| 6 | + * Each node contains a single digit, and digits are stored in forward order. |
| 7 | + * The algorithm works by reversing the lists, multiplying digit by digit, |
| 8 | + * shifting partial results according to place value, adding them together, |
| 9 | + * and finally reversing the result to get the correct order. |
| 10 | + * |
| 11 | + * Use Cases: |
| 12 | + * - Multiplying very large numbers that cannot fit into primitive data types |
| 13 | + * - Working with numbers stored in linked list format in memory-efficient |
| 14 | + * applications |
| 15 | + * |
| 16 | + * Time Complexity: O(m * n) |
| 17 | + * - m = number of nodes in l1 |
| 18 | + * - n = number of nodes in l2 |
| 19 | + * - Each digit of l2 is multiplied with all digits of l1, and partial sums are |
| 20 | + * added |
| 21 | + * |
| 22 | + * Space Complexity: O(m + n) |
| 23 | + * - For storing the result linked list and intermediate partial products |
| 24 | + */ |
| 25 | + |
| 26 | +class ListNode { |
| 27 | + int data; |
| 28 | + ListNode next; |
| 29 | + |
| 30 | + ListNode(int data) { |
| 31 | + this.data = data; |
| 32 | + this.next = null; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +public class Multiply_TwoNums_LL { |
| 37 | + |
| 38 | + /** |
| 39 | + * Multiplies two numbers represented as linked lists |
| 40 | + */ |
| 41 | + public ListNode multiplyTwoNumbers(ListNode l1, ListNode l2) { |
| 42 | + if (l1 == null || l2 == null) |
| 43 | + return new ListNode(0); |
| 44 | + |
| 45 | + l1 = reverseList(l1); // Reverse to start from least significant digit |
| 46 | + l2 = reverseList(l2); |
| 47 | + |
| 48 | + ListNode result = new ListNode(0); |
| 49 | + ListNode l2Ptr = l2; |
| 50 | + int l2Pos = 0; |
| 51 | + |
| 52 | + while (l2Ptr != null) { |
| 53 | + // Multiply l1 with current digit of l2 |
| 54 | + ListNode partialProduct = multiplyWithSingleDigit(l1, l2Ptr.data); |
| 55 | + |
| 56 | + // Add zeros to shift the partial product |
| 57 | + partialProduct = addZeros(partialProduct, l2Pos); |
| 58 | + |
| 59 | + // Add partial product to cumulative result |
| 60 | + result = addTwoNumbers(result, partialProduct); |
| 61 | + |
| 62 | + l2Ptr = l2Ptr.next; |
| 63 | + l2Pos++; |
| 64 | + } |
| 65 | + |
| 66 | + return reverseList(result.next); // Reverse final result to correct order |
| 67 | + } |
| 68 | + |
| 69 | + private ListNode multiplyWithSingleDigit(ListNode l, int digit) { |
| 70 | + ListNode dummy = new ListNode(0); |
| 71 | + ListNode current = dummy; |
| 72 | + int carry = 0; |
| 73 | + |
| 74 | + while (l != null || carry != 0) { |
| 75 | + int sum = carry + (l != null ? l.data * digit : 0); |
| 76 | + carry = sum / 10; |
| 77 | + current.next = new ListNode(sum % 10); |
| 78 | + current = current.next; |
| 79 | + if (l != null) |
| 80 | + l = l.next; |
| 81 | + } |
| 82 | + |
| 83 | + return dummy.next; |
| 84 | + } |
| 85 | + |
| 86 | + private ListNode addZeros(ListNode l, int count) { |
| 87 | + while (count-- > 0) { |
| 88 | + ListNode newNode = new ListNode(0); |
| 89 | + newNode.next = l; |
| 90 | + l = newNode; |
| 91 | + } |
| 92 | + return l; |
| 93 | + } |
| 94 | + |
| 95 | + private ListNode addTwoNumbers(ListNode l1, ListNode l2) { |
| 96 | + ListNode dummy = new ListNode(0); |
| 97 | + ListNode current = dummy; |
| 98 | + int carry = 0; |
| 99 | + |
| 100 | + while (l1 != null || l2 != null || carry != 0) { |
| 101 | + int sum = carry + (l1 != null ? l1.data : 0) + (l2 != null ? l2.data : 0); |
| 102 | + carry = sum / 10; |
| 103 | + current.next = new ListNode(sum % 10); |
| 104 | + current = current.next; |
| 105 | + if (l1 != null) |
| 106 | + l1 = l1.next; |
| 107 | + if (l2 != null) |
| 108 | + l2 = l2.next; |
| 109 | + } |
| 110 | + |
| 111 | + return dummy.next; |
| 112 | + } |
| 113 | + |
| 114 | + private ListNode reverseList(ListNode head) { |
| 115 | + ListNode prev = null, current = head; |
| 116 | + while (current != null) { |
| 117 | + ListNode next = current.next; |
| 118 | + current.next = prev; |
| 119 | + prev = current; |
| 120 | + current = next; |
| 121 | + } |
| 122 | + return prev; |
| 123 | + } |
| 124 | + |
| 125 | + public static void printList(ListNode head) { |
| 126 | + while (head != null) { |
| 127 | + System.out.print(head.data); |
| 128 | + head = head.next; |
| 129 | + } |
| 130 | + System.out.println(); |
| 131 | + } |
| 132 | + |
| 133 | + public static void main(String[] args) { |
| 134 | + // Test case 1: 342 * 465 = 159030 |
| 135 | + ListNode l1 = new ListNode(3); |
| 136 | + l1.next = new ListNode(4); |
| 137 | + l1.next.next = new ListNode(2); |
| 138 | + |
| 139 | + ListNode l2 = new ListNode(4); |
| 140 | + l2.next = new ListNode(6); |
| 141 | + l2.next.next = new ListNode(5); |
| 142 | + |
| 143 | + Multiply_TwoNums_LL mt = new Multiply_TwoNums_LL(); |
| 144 | + ListNode result = mt.multiplyTwoNumbers(l1, l2); |
| 145 | + |
| 146 | + System.out.print("Product: "); |
| 147 | + printList(result); |
| 148 | + } |
| 149 | +} |
0 commit comments