substract polynomials

This commit is contained in:
Krystian Madra 2018-06-28 00:31:07 +02:00
parent 14bd8e6827
commit 8bc941ab4e

View File

@ -134,5 +134,34 @@ public class Main {
}
return result;
}
}
public static LinkedList<Integer> substractPolynomials(LinkedList<Integer> p1, LinkedList<Integer> p2){
LinkedList<Integer> result = new LinkedList<Integer>();
LinkedList<Integer> p2Copy = new LinkedList<Integer>();
for(int i=0; i<p2.size(); i++) {
p2Copy.add(p2.get(i));
}
while(!p2Copy.isEmpty()) {
result.add(p1.poll() - p2Copy.poll());
}
while(!p1.isEmpty()) {
result.add(p1.poll());
}
while(result.getLast() == 0) {
if(result.size()>1) {
result.removeLast();
}
else break;
}
return result;
}
}