|
| 1 | +1. You are given two linked lists. Data of each node of these linked lists contain a digit from the range: [0, 9]. |
| 2 | +2. The linked lists represent two numbers. |
| 3 | +3. You have to print the product of these two numbers. |
| 4 | +Input Format: |
| 5 | +Input has already been managed for cpp and java submissions. In cpp and java, you have to just write a function which receives head nodes of two linked lists. The sizes of linked lists are N and M, respectively. |
| 6 | +For other languages, the first line of input contains list of N space separated integers, terminated by -1. The following line of input also contains list of M space separated integers, terminated by -1. The integers form the data of nodes of two linked list. |
| 7 | +Constraints: |
| 8 | +N and M lies in the range: [1, 1000]. |
| 9 | +0 <= |Node Data| <= 9 |
| 10 | +Output format: |
| 11 | +Print the product of numbers, represented by two linked lists. |
| 12 | +Sample Input: |
| 13 | +1 2 3 4 5 -1 |
| 14 | +1 2 3 -1 |
| 15 | +Sample Output: |
| 16 | +1 5 1 8 4 3 5 |
| 17 | + |
| 18 | +**************************************Code**************************************** |
| 19 | + |
| 20 | +public class Solution { |
| 21 | + public static LinkedListNode<Integer> temphead; |
| 22 | + |
| 23 | + public static void multiply(LinkedListNode<Integer> head1, LinkedListNode<Integer> head2) { |
| 24 | + //Your code goes here |
| 25 | + LinkedListNode<Integer> print = multiplyLL(head1, head2); |
| 26 | + while (print != null) { |
| 27 | + System.out.print(print.data + " "); |
| 28 | + print = print.next; |
| 29 | + } |
| 30 | + |
| 31 | + } |
| 32 | + public static int reverse(LinkedListNode<Integer> head) |
| 33 | + { |
| 34 | + LinkedListNode<Integer> prev=null; |
| 35 | + LinkedListNode<Integer> curr=head; |
| 36 | + int size=0; |
| 37 | + LinkedListNode<Integer> ahead=null; |
| 38 | + while(curr!=null) |
| 39 | + { |
| 40 | + size++; |
| 41 | + ahead=curr.next; |
| 42 | + curr.next=prev; |
| 43 | + prev=curr; |
| 44 | + curr=ahead; |
| 45 | + } |
| 46 | + head=prev; |
| 47 | + temphead=head; |
| 48 | + return size; |
| 49 | + } |
| 50 | + public static LinkedListNode<Integer> makeEmpty(int len) |
| 51 | + { |
| 52 | + LinkedListNode<Integer> head=null; |
| 53 | + LinkedListNode<Integer> trav=null; |
| 54 | + while(len>0) |
| 55 | + { |
| 56 | + LinkedListNode<Integer> temp=new LinkedListNode<>(0); |
| 57 | + if(head==null) |
| 58 | + { |
| 59 | + head=temp; |
| 60 | + trav=temp; |
| 61 | + } |
| 62 | + else{ |
| 63 | + trav.next=temp; |
| 64 | + trav=trav.next; |
| 65 | + } |
| 66 | + len--; |
| 67 | + } |
| 68 | + return head; |
| 69 | + } |
| 70 | + public static LinkedListNode<Integer> multiplyLL(LinkedListNode<Integer> head1, LinkedListNode<Integer> head2) |
| 71 | + { |
| 72 | + int m=reverse(head1); |
| 73 | + head1=temphead; |
| 74 | + int n=reverse(head2); |
| 75 | + head2=temphead; |
| 76 | + LinkedListNode<Integer> res=makeEmpty(m+n); |
| 77 | + LinkedListNode<Integer> second=head2, resptr1=res, resptr2, firstptr; |
| 78 | + while(second!=null) |
| 79 | + { |
| 80 | + int cnt=0; |
| 81 | + resptr2=resptr1; |
| 82 | + firstptr=head1; |
| 83 | + while(firstptr!=null) |
| 84 | + { |
| 85 | + int mul=firstptr.data*second.data+cnt; |
| 86 | + resptr2.data+=mul%10; |
| 87 | + cnt=mul/10+ resptr2.data/10; |
| 88 | + resptr2.data=resptr2.data %10; |
| 89 | + firstptr=firstptr.next; |
| 90 | + resptr2=resptr2.next; |
| 91 | + } |
| 92 | + if(cnt>0) |
| 93 | + resptr2.data+=cnt; |
| 94 | + resptr1=resptr1.next; |
| 95 | + second=second.next; |
| 96 | + } |
| 97 | + reverse(res); |
| 98 | + res=temphead; |
| 99 | + while(res.next!=null && res.data==0) |
| 100 | + { |
| 101 | + LinkedListNode<Integer> temp=res; |
| 102 | + res=res.next; |
| 103 | + } |
| 104 | + return res; |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments