-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest6-Multiply Two Linked Lists
107 lines (102 loc) · 3.44 KB
/
Test6-Multiply Two Linked Lists
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
1. You are given two linked lists. Data of each node of these linked lists contain a digit from the range: [0, 9].
2. The linked lists represent two numbers.
3. You have to print the product of these two numbers.
Input Format:
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.
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.
Constraints:
N and M lies in the range: [1, 1000].
0 <= |Node Data| <= 9
Output format:
Print the product of numbers, represented by two linked lists.
Sample Input:
1 2 3 4 5 -1
1 2 3 -1
Sample Output:
1 5 1 8 4 3 5
**************************************Code****************************************
public class Solution {
public static LinkedListNode<Integer> temphead;
public static void multiply(LinkedListNode<Integer> head1, LinkedListNode<Integer> head2) {
//Your code goes here
LinkedListNode<Integer> print = multiplyLL(head1, head2);
while (print != null) {
System.out.print(print.data + " ");
print = print.next;
}
}
public static int reverse(LinkedListNode<Integer> head)
{
LinkedListNode<Integer> prev=null;
LinkedListNode<Integer> curr=head;
int size=0;
LinkedListNode<Integer> ahead=null;
while(curr!=null)
{
size++;
ahead=curr.next;
curr.next=prev;
prev=curr;
curr=ahead;
}
head=prev;
temphead=head;
return size;
}
public static LinkedListNode<Integer> makeEmpty(int len)
{
LinkedListNode<Integer> head=null;
LinkedListNode<Integer> trav=null;
while(len>0)
{
LinkedListNode<Integer> temp=new LinkedListNode<>(0);
if(head==null)
{
head=temp;
trav=temp;
}
else{
trav.next=temp;
trav=trav.next;
}
len--;
}
return head;
}
public static LinkedListNode<Integer> multiplyLL(LinkedListNode<Integer> head1, LinkedListNode<Integer> head2)
{
int m=reverse(head1);
head1=temphead;
int n=reverse(head2);
head2=temphead;
LinkedListNode<Integer> res=makeEmpty(m+n);
LinkedListNode<Integer> second=head2, resptr1=res, resptr2, firstptr;
while(second!=null)
{
int cnt=0;
resptr2=resptr1;
firstptr=head1;
while(firstptr!=null)
{
int mul=firstptr.data*second.data+cnt;
resptr2.data+=mul%10;
cnt=mul/10+ resptr2.data/10;
resptr2.data=resptr2.data %10;
firstptr=firstptr.next;
resptr2=resptr2.next;
}
if(cnt>0)
resptr2.data+=cnt;
resptr1=resptr1.next;
second=second.next;
}
reverse(res);
res=temphead;
while(res.next!=null && res.data==0)
{
LinkedListNode<Integer> temp=res;
res=res.next;
}
return res;
}
}