|
| 1 | +class ListNode { |
| 2 | + int data; |
| 3 | + ListNode next; |
| 4 | + |
| 5 | + ListNode(int data) { |
| 6 | + this.data = data; |
| 7 | + this.next = null; |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +public class MultiplyTwoNumbersLL { |
| 12 | + |
| 13 | + // Multiply two numbers represented as linked lists |
| 14 | + public ListNode multiplyTwoNumbers(ListNode l1, ListNode l2) { |
| 15 | + if (l1 == null || l2 == null) |
| 16 | + return new ListNode(0); |
| 17 | + |
| 18 | + l1 = reverseList(l1); |
| 19 | + l2 = reverseList(l2); |
| 20 | + |
| 21 | + ListNode result = new ListNode(0); |
| 22 | + ListNode l2Ptr = l2; |
| 23 | + int l2Pos = 0; |
| 24 | + |
| 25 | + while (l2Ptr != null) { |
| 26 | + ListNode partialProduct = multiplyWithSingleDigit(l1, l2Ptr.data); |
| 27 | + partialProduct = addZeros(partialProduct, l2Pos); |
| 28 | + result = addTwoNumbers(result, partialProduct); |
| 29 | + l2Ptr = l2Ptr.next; |
| 30 | + l2Pos++; |
| 31 | + } |
| 32 | + |
| 33 | + return reverseList(result.next); |
| 34 | + } |
| 35 | + |
| 36 | + // Multiply a linked list with a single digit |
| 37 | + private ListNode multiplyWithSingleDigit(ListNode l, int digit) { |
| 38 | + ListNode dummy = new ListNode(0); |
| 39 | + ListNode current = dummy; |
| 40 | + int carry = 0; |
| 41 | + |
| 42 | + while (l != null || carry != 0) { |
| 43 | + int sum = carry + (l != null ? l.data * digit : 0); |
| 44 | + carry = sum / 10; |
| 45 | + current.next = new ListNode(sum % 10); |
| 46 | + current = current.next; |
| 47 | + if (l != null) |
| 48 | + l = l.next; |
| 49 | + } |
| 50 | + |
| 51 | + return dummy.next; |
| 52 | + } |
| 53 | + |
| 54 | + // Add leading zeros for place value shift |
| 55 | + private ListNode addZeros(ListNode l, int count) { |
| 56 | + while (count-- > 0) { |
| 57 | + ListNode newNode = new ListNode(0); |
| 58 | + newNode.next = l; |
| 59 | + l = newNode; |
| 60 | + } |
| 61 | + return l; |
| 62 | + } |
| 63 | + |
| 64 | + // Add two linked list numbers |
| 65 | + private ListNode addTwoNumbers(ListNode l1, ListNode l2) { |
| 66 | + ListNode dummy = new ListNode(0); |
| 67 | + ListNode current = dummy; |
| 68 | + int carry = 0; |
| 69 | + |
| 70 | + while (l1 != null || l2 != null || carry != 0) { |
| 71 | + int sum = carry + (l1 != null ? l1.data : 0) + (l2 != null ? l2.data : 0); |
| 72 | + carry = sum / 10; |
| 73 | + current.next = new ListNode(sum % 10); |
| 74 | + current = current.next; |
| 75 | + if (l1 != null) |
| 76 | + l1 = l1.next; |
| 77 | + if (l2 != null) |
| 78 | + l2 = l2.next; |
| 79 | + } |
| 80 | + return dummy.next; |
| 81 | + } |
| 82 | + |
| 83 | + // Reverse a linked list |
| 84 | + private ListNode reverseList(ListNode head) { |
| 85 | + ListNode prev = null, current = head; |
| 86 | + while (current != null) { |
| 87 | + ListNode next = current.next; |
| 88 | + current.next = prev; |
| 89 | + prev = current; |
| 90 | + current = next; |
| 91 | + } |
| 92 | + return prev; |
| 93 | + } |
| 94 | + |
| 95 | + // Helper function to print linked list |
| 96 | + public static void printList(ListNode head) { |
| 97 | + while (head != null) { |
| 98 | + System.out.print(head.data); |
| 99 | + head = head.next; |
| 100 | + } |
| 101 | + System.out.println(); |
| 102 | + } |
| 103 | + |
| 104 | + // Example usage |
| 105 | + public static void main(String[] args) { |
| 106 | + // Number 342 |
| 107 | + ListNode l1 = new ListNode(3); |
| 108 | + l1.next = new ListNode(4); |
| 109 | + l1.next.next = new ListNode(2); |
| 110 | + |
| 111 | + // Number 465 |
| 112 | + ListNode l2 = new ListNode(4); |
| 113 | + l2.next = new ListNode(6); |
| 114 | + l2.next.next = new ListNode(5); |
| 115 | + |
| 116 | + MultiplyTwoNumbersLL mt = new MultiplyTwoNumbersLL(); |
| 117 | + ListNode result = mt.multiplyTwoNumbers(l1, l2); |
| 118 | + |
| 119 | + System.out.print("Product: "); |
| 120 | + printList(result); // Expected output: 159030 |
| 121 | + } |
| 122 | +} |
0 commit comments