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+
126class ListNode {
227 int data ;
328 ListNode next ;
@@ -8,32 +33,39 @@ class ListNode {
833 }
934}
1035
11- public class MultiplyTwoNumbersLL {
36+ public class Multiply_TwoNumbers_LL {
1237
13- // Multiply two numbers represented as linked lists
38+ /**
39+ * Multiplies two numbers represented as linked lists
40+ */
1441 public ListNode multiplyTwoNumbers (ListNode l1 , ListNode l2 ) {
1542 if (l1 == null || l2 == null )
1643 return new ListNode (0 );
1744
18- l1 = reverseList (l1 );
45+ l1 = reverseList (l1 ); // Reverse to start from least significant digit
1946 l2 = reverseList (l2 );
2047
2148 ListNode result = new ListNode (0 );
2249 ListNode l2Ptr = l2 ;
2350 int l2Pos = 0 ;
2451
2552 while (l2Ptr != null ) {
53+ // Multiply l1 with current digit of l2
2654 ListNode partialProduct = multiplyWithSingleDigit (l1 , l2Ptr .data );
55+
56+ // Add zeros to shift the partial product
2757 partialProduct = addZeros (partialProduct , l2Pos );
58+
59+ // Add partial product to cumulative result
2860 result = addTwoNumbers (result , partialProduct );
61+
2962 l2Ptr = l2Ptr .next ;
3063 l2Pos ++;
3164 }
3265
33- return reverseList (result .next );
66+ return reverseList (result .next ); // Reverse final result to correct order
3467 }
3568
36- // Multiply a linked list with a single digit
3769 private ListNode multiplyWithSingleDigit (ListNode l , int digit ) {
3870 ListNode dummy = new ListNode (0 );
3971 ListNode current = dummy ;
@@ -51,7 +83,6 @@ private ListNode multiplyWithSingleDigit(ListNode l, int digit) {
5183 return dummy .next ;
5284 }
5385
54- // Add leading zeros for place value shift
5586 private ListNode addZeros (ListNode l , int count ) {
5687 while (count -- > 0 ) {
5788 ListNode newNode = new ListNode (0 );
@@ -61,7 +92,6 @@ private ListNode addZeros(ListNode l, int count) {
6192 return l ;
6293 }
6394
64- // Add two linked list numbers
6595 private ListNode addTwoNumbers (ListNode l1 , ListNode l2 ) {
6696 ListNode dummy = new ListNode (0 );
6797 ListNode current = dummy ;
@@ -77,10 +107,10 @@ private ListNode addTwoNumbers(ListNode l1, ListNode l2) {
77107 if (l2 != null )
78108 l2 = l2 .next ;
79109 }
110+
80111 return dummy .next ;
81112 }
82113
83- // Reverse a linked list
84114 private ListNode reverseList (ListNode head ) {
85115 ListNode prev = null , current = head ;
86116 while (current != null ) {
@@ -92,7 +122,6 @@ private ListNode reverseList(ListNode head) {
92122 return prev ;
93123 }
94124
95- // Helper function to print linked list
96125 public static void printList (ListNode head ) {
97126 while (head != null ) {
98127 System .out .print (head .data );
@@ -101,14 +130,12 @@ public static void printList(ListNode head) {
101130 System .out .println ();
102131 }
103132
104- // Example usage
105133 public static void main (String [] args ) {
106- // Number 342
134+ // Test case 1: 342 * 465 = 159030
107135 ListNode l1 = new ListNode (3 );
108136 l1 .next = new ListNode (4 );
109137 l1 .next .next = new ListNode (2 );
110138
111- // Number 465
112139 ListNode l2 = new ListNode (4 );
113140 l2 .next = new ListNode (6 );
114141 l2 .next .next = new ListNode (5 );
@@ -117,6 +144,6 @@ public static void main(String[] args) {
117144 ListNode result = mt .multiplyTwoNumbers (l1 , l2 );
118145
119146 System .out .print ("Product: " );
120- printList (result ); // Expected output: 159030
147+ printList (result );
121148 }
122149}
0 commit comments