substract polynomials
This commit is contained in:
parent
14bd8e6827
commit
8bc941ab4e
31
Main.java
31
Main.java
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user