This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I'm writing a program that creates an infinitely long integer by making each number in the integer its own node.
For example: 123,456 = [1]->[2]->[3]->[4]->[5]->[6]
Also, I need to be able to add the contents of two infinite integers together.
For example: [1]->[2]->[3]->[4]->[5]->[6] + [1]->[2]->[3]
= [1]->[2]->[3]->[5]->[7]->[9]
However, when I try to reversely traverse one of the linked lists, I get a NullPointerException after the first loop through;
while((currentOne != null) && (currentTwo != null)) {
// for(int i = firstOperand.getNumberOfDigitsWithZeroes(); i>0; i--)
if(currentOne.data > currentTwo.data) {
tempResult = currentOne.data - currentTwo.data;
//result = tempResult + result;
returnResult.addToFront(tempResult);
} else if(currentOne.data < currentTwo.data) {
currentOne.previous.data -= 1;
currentOne.data += 10;
tempResult = currentOne.data - currentTwo.data;
//result = tempResult + result;
returnResult.addToFront(tempResult);
} else if((currentOne == firstOperand.firstNode && currentTwo == secondOperand.firstNode) &&
currentOne.data - currentTwo.data == 0) {
break;
} else {
returnResult.addToFront(0);
}
currentOne = currentOne.previous;
currentTwo = currentTwo.previous;
}
firstOperand is the linked list of the number 100000000000000000000. currentOne is a node initialized to the last node of the firstOperand. currentTwo is a node initialized to the last node of the secondOperand which has the contents of 000000000000000000001. The error is located in the line
if(currentOne.data > currentTwo.data)
The error occurs after only the second run through of the loop, and i'm not sure why, because when printed in string form, the secondOperand shows the full 000000000000000000001. So, I'm not sure why i'm getting the null pointer exception instead of currentTwo.data being equal to 0.
Here is the constructor for an InfiniteInteger object
public LInfiniteInteger(String s)
{
// TO DO
int newDigit = 0;
isNegative = false;
numberOfDigits = 0;
middlePosition = 0;
firstNode = null;
middleNode = null;
lastNode =null;
for(int i =0; i<s.length(); i++)
{
newDigit = (int)s.charAt(i) - 48;
if((newDigit >= 0) && (newDigit <= 9))
{
// this.add(newDigit);
if(firstNode == null)
{
firstNode = new Node(null, newDigit, null);
lastNode = firstNode;
}
else
{
Node newNode = new Node(lastNode, newDigit, null);
lastNode.next = newNode;
lastNode = newNode;
}
numberOfDigits++;
if(numberOfDigits %2 == 1);
{
if(middleNode == null)
{
middleNode = firstNode;
}
else
{
middleNode = middleNode.next;
}
middlePosition++;
}
}
else if((newDigit == ((int)'-') - 48))
{
isNegative = true;
}
}
}
This is the full method of where the error occurs:
public InfiniteIntegerInterface minus(final InfiniteIntegerInterface anInfiniteInteger)
{
// TO DO
LInfiniteInteger firstOperand = new LInfiniteInteger(this.toString());
LInfiniteInteger secondOperand = new LInfiniteInteger(anInfiniteInteger.toString());
LInfiniteInteger returnResult = new LInfiniteInteger("");
int tempResult;
boolean resultIsNegative = false;
Node currentOne = firstOperand.lastNode;
Node currentTwo = secondOperand.lastNode;
while(firstOperand.getNumberOfDigitsWithZeroes() > secondOperand.getNumberOfDigitsWithZeroes())
{
Node temp = new Node(0);
temp.next=secondOperand.firstNode;
secondOperand.firstNode = temp;
secondOperand.addNumberOfDigits();
}
while(firstOperand.getNumberOfDigitsWithZeroes() < secondOperand.getNumberOfDigitsWithZeroes())
{
Node temp = new Node(0);
temp.next=firstOperand.firstNode;
firstOperand.firstNode = temp;
firstOperand.addNumberOfDigits();
}
if((firstOperand.isNegative == false) && (secondOperand.isNegative == false))
{
if(firstOperand.compareMag(secondOperand) == 1)
{
//algorithm
//System.out.println(currentTwo.data);
//System.out.println(secondOperand.toString());
for(int i = secondOperand.getNumberOfDigitsWithZeroes(); i > 0; i--)
{
System.out.println(currentOne.data);
currentOne = currentOne.previous;
}
currentOne = firstOperand.lastNode;
while((currentOne != null) && (currentTwo != null))
// for(int i = firstOperand.getNumberOfDigitsWithZeroes(); i>0; i--)
{
if(currentOne.data > currentTwo.data)
{
tempResult = currentOne.data - currentTwo.data;
//result = tempResult + result;
returnResult.addToFront(tempResult);
}
else if(currentOne.data < currentTwo.data)
{
currentOne.previous.data -= 1;
currentOne.data += 10;
tempResult = currentOne.data - currentTwo.data;
//result = tempResult + result;
returnResult.addToFront(tempResult);
}
else if((currentOne == firstOperand.firstNode && currentTwo == secondOperand.firstNode) && currentOne.data - currentTwo.data == 0)
{
break;
}
else
{
returnResult.addToFront(0);
}
currentOne = currentOne.previous;
currentTwo = currentTwo.previous;
}
if(currentOne == null)
{
while(currentTwo != null)
{
//result = currentTwo.data + result;
returnResult.addToFront(currentTwo.data);
currentTwo = currentTwo.previous;
}
}
else if(currentTwo == null)
{
while(currentTwo != null)
{
//result = currentTwo.data + result;
returnResult.addToFront(currentTwo.data);
currentTwo = currentTwo.previous;
}
}
return returnResult;
}
else if(firstOperand.compareMag(secondOperand) == 0)
{
returnResult.add(0);
return returnResult;
}
else
{
LInfiniteInteger tempReturnResult = new LInfiniteInteger(secondOperand.minus(firstOperand).toString());
Node currentOneInner = tempReturnResult.firstNode;
while(currentOneInner != null)
{
returnResult.add(currentOneInner.data);
currentOneInner = currentOneInner.next;
}
returnResult.isNegative = true;
return returnResult;
}
}
else if((firstOperand.isNegative == false) && (secondOperand.isNegative == true))
{
secondOperand.isNegative = false;
LInfiniteInteger tempReturnResult = new LInfiniteInteger(firstOperand.plus(secondOperand).toString());
Node currentOneInner = tempReturnResult.firstNode;
while(currentOneInner != null)
{
returnResult.add(currentOneInner.data);
currentOneInner = currentOneInner.next;
}
return returnResult;
}
else if((firstOperand.isNegative == true) && (secondOperand.isNegative == false))
{
firstOperand.isNegative = false;
LInfiniteInteger tempReturnResult = new LInfiniteInteger(firstOperand.plus(secondOperand).toString());
Node currentOneInner = tempReturnResult.firstNode;
while(currentOneInner != null)
{
returnResult.add(currentOneInner.data);
currentOneInner = currentOneInner.next;
}
returnResult.isNegative = true;
return returnResult;
}
else
{
if(firstOperand.compareMag(secondOperand) == -1)
{
firstOperand.isNegative = false;
secondOperand.isNegative = false;
LInfiniteInteger tempReturnResult = new LInfiniteInteger(firstOperand.minus(secondOperand).toString());
Node currentOneInner = tempReturnResult.firstNode;
while(currentOneInner != null)
{
returnResult.add(currentOneInner.data);
currentOneInner = currentOneInner.next;
}
return returnResult;
}
else if(firstOperand.compareMag(secondOperand) == 0)
{
returnResult.add(0);
return returnResult;
}
else
{
firstOperand.isNegative = false;
secondOperand.isNegative = false;
LInfiniteInteger tempReturnResult = new LInfiniteInteger(firstOperand.minus(secondOperand).toString());
Node currentOneInner = tempReturnResult.firstNode;
while(currentOneInner != null)
{
returnResult.add(currentOneInner.data);
currentOneInner = currentOneInner.next;
}
returnResult.isNegative = true;
return returnResult;
}
}
}
I'm trying to subtract 1000000000000000 and 0000000000000001.
Here's my Node class:
private class Node
{
private int data;
private Node next;
private Node previous;
private Node(Node previousNode, int aData, Node nextNode)
{
previous = previousNode;
data = aData;
next = nextNode;
}
private Node(int aData)
{
this(null, aData, null);
}
}
The problem may happens because you fail to initialize the member variable data. Check your code carefully and debug it step by step.
Your constructor doesn't take care of member previous, or is this in the Node constructor?
Related
I am trying to implement a max priority queue using a heap binary tree with a triple-linked node. This is the code that I currently have yet when I run it and try to print out the tree nothing prints out it is just empty lines. I am using the helped methods sink and swim in order to help me organize the queue as I add different elements. I am also implementing an ADT (MaxPQ) which just has the public methods that need to be implemented. I was wondering if there is anything that I am doing wrong?
public class LinkedMaxPQ<T extends Comparable<T>> implements MaxPQ<T> {
// Instance variables
Node root;
int size;
Node lastInserted;
// Node inner class definition
// Node class
class Node {
int N;
T info;
Node left;
Node right;
Node parent;
Node(T info, int N) {
this.info = info; this.N = N;
}
}
private void swim(Node x){
if(x == null) return;
if(x.parent == null) return; // we're at root
int cmp = x.info.compareTo(x.parent.info);
if(cmp > 0){
swapNodeData(x, x.parent);
swim(x.parent);
}
}
private void swapNodeData(Node x, Node y){
T temp = x.info;
x.info = y.info;
y.info = temp;
}
private void sink(Node x){
if(x == null) return;
Node swapNode;
if(x.left == null && x.right == null){
return;
}
else if(x.left == null){
swapNode = x.right;
int cmp = x.info.compareTo(swapNode.info);
if(cmp < 0)
swapNodeData(swapNode, x);
} else if(x.right == null){
swapNode = x.left;
int cmp = x.info.compareTo(swapNode.info);
if(cmp < 0)
swapNodeData(swapNode, x);
} else{
int cmp = x.left.info.compareTo(x.right.info);
if(cmp >= 0){
swapNode = x.left;
} else{
swapNode = x.right;
}
int cmpParChild = x.info.compareTo(swapNode.info);
if(cmpParChild < 0) {
swapNodeData(swapNode, x);
sink(swapNode);
}
}
}
String printThisLevel (Node rootnode, int level) {
StringBuilder s = new StringBuilder();
// Base case 1: if the current rootnode is null, return the current string.
if (rootnode == null) {
return s.toString();
}
// Base case 2: If you're at the first level, append the
// info field of the current rootnode.
if (level == 1) {
s.append( rootnode.info.toString());
}
// Recursive calls: otherwise call the method on the left
// and on the right of the next lower level.
else if (level > 1) {
s.append( printThisLevel(rootnode.left, level-1));
s.append( printThisLevel(rootnode.right, level-1));
}
return s.toString();
}
private int size(Node x){
if(x == null) return 0;
return x.N;
}
private Node insert(Node x, T data){
if(x == null){
lastInserted = new Node(data, 1);
return lastInserted;
}
// compare left and right sizes see where to go
int leftSize = size(x.left);
int rightSize = size(x.right);
if(leftSize <= rightSize){
// go to left
Node inserted = insert(x.left, data);
x.left = inserted;
inserted.parent = x;
} else{
// go to right
Node inserted = insert(x.right, data);
x.right = inserted;
inserted.parent = x;
}
x.N = size(x.left) + size(x.right) + 1;
return x;
}
private Node resetLastInserted(Node x){
if(x == null) return null;
if(x.left == null && x.right == null) return x;
if(size(x.right) < size(x.left))return resetLastInserted(x.left);
else return resetLastInserted(x.right);
}
public void insert(T data){
root = insert(root, data);
swim(lastInserted);
}
public T getMax(){
if(root == null) return null;
return root.info;
}
public T removeMax(){
if(size() == 1){
T ret = root.info;
root = null;
return ret;
}
swapNodeData(root, lastInserted);
Node lastInsParent = lastInserted.parent;
T lastInsData = lastInserted.info;
if(lastInserted == lastInsParent.left){
lastInsParent.left = null;
} else{
lastInsParent.right = null;
}
Node traverser = lastInserted;
while(traverser != null){
traverser.N--;
traverser = traverser.parent;
}
lastInserted = resetLastInserted(root);
sink(root);
return lastInsData;
}
public int size(){
return size(root);
}
public boolean isEmpty(){
return size() == 0;
}
public String toString() {
// Create a StringBuilder object to make it more efficient.
StringBuilder sb=new StringBuilder();
// get the height of the tree
int height = (int)Math.ceil(Math.log(size+1) / Math.log(2));
// for each level in the tree, call printThisLevel and
// append the output to the StringBuilder
for (int i=1; i<=height; i++) {
sb.append("level " + i + ": "+ printThisLevel(this.root, i) + "\n");
}
// Return the string of the StringBuilder object
return sb.toString();
}
public static void main (String[] args) {
LinkedMaxPQ<String> t = new LinkedMaxPQ<String>();
t.insert("a");
System.out.println(t.toString());
t.insert("b");
t.insert("c");
t.insert("d");
t.insert("e");
t.insert("f");
t.insert("g");
t.insert("h");
t.insert("i");
t.insert("j");
t.insert("k");
t.size();
t.removeMax();
t.getMax();
t.removeMax();
t.insert("x");
t.insert("y");
t.removeMax();
t.getMax();
System.out.println(t.toString());
}
}
In this line:
int height = (int)Math.ceil(Math.log(size+1) / Math.log(2));
size should be size().
int height = (int)Math.ceil(Math.log(size()+1) / Math.log(2));
After this correction, the results are coming out.
However, there is a logic problem, which needs a solution.
For test case, testdata = new int[] {3, 5, 2, -7, 9, 4, 7};
The result is 9 4 7 -7 3 2 5
But correct result should be 9 5 7 -7 3 2 4 (from another array implementation).
I know the mistake comes from when at the 3rd levle, insert data {9}, its parent should be the 2nd leverl data {3} on the left, not the {2} on the right. Any thought to solve it?
I am trying to remove a string from a BST and I cannot figure it out. I know how to remove integers but cannot convert my code to remove Nodes that have strings. Here is the code I currently have
/*
* Removes the specified string from the BST
*/
public boolean remove(String s){
Node ans = remove(root, s);
if(ans == null){
return false;
} else {
return true;
}
}
private static Node remove(Node root, String s) {
if (root == null)
return null;
if (s.compareTo(root.data) < 0) {
root.left = remove(root.left, s);
} else if (s.compareTo(root.data) > 0) {
root.right = remove(root.right, s);
} else {
if (root.left == null) {
return root.right;
} else if (root.right == null)
return root.left;
root.data = minimum(root.right);
root.right = remove(root.right, root.data);
}
return root;
}
public static String minimum(Node root) {
String minimum = root.data;
while (root.left != null) {
minimum = root.left.data;
root = root.left;
}
return minimum;
}
Replace all "Integer" with "String" .
I know this was 2 years ago but I case someone needs help I did this instead..
private String minimum(BSTNode treeRoot) {
String minimum = treeRoot.value;
while (treeRoot.left != null) {
minimum = treeRoot.left.value;
treeRoot = treeRoot.left;
}
return minimum;
}
private BSTNode removeValue(BSTNode treeRoot, String s){
if(treeRoot == null){
return treeRoot;
} else if(s.compareTo(treeRoot.value) < 0){
treeRoot.left = removeValue(treeRoot.left, s);
} else if(s.compareTo(treeRoot.value) > 0){
treeRoot.right = removeValue(treeRoot.right, s);
} else {
if(treeRoot.left == null){
return treeRoot.right;
} else if(treeRoot.right == null){
return treeRoot.left;
}
treeRoot.value = minimum(treeRoot.right);
treeRoot.right = removeValue(treeRoot.right, treeRoot.value);
}
return treeRoot;
}
I am writing a compararator to sort log lines:
Code:
public static final Comparator<String> HTMLcomparator = new Comparator<String>()
{
#Override
public int compare(String line1, String line2)
{
HTMLLogLine htmlLogLine1 = new HTMLLogLine(line1);
HTMLLogLine htmlLogLine2 = new HTMLLogLine(line2);
int fullCompare = 0;
String requestId1 = htmlLogLine1.getRequestId();
String requestId2 = htmlLogLine2.getRequestId();
if(requestId1 != null && requestId2 != null)
{
fullCompare = requestId1.compareTo(requestId2);
}
else if(requestId1 == null && requestId2 != null)
{
fullCompare = -1;
}
else if(requestId1 != null)
{
fullCompare = 1;
}
else
{
fullCompare = 0;
}
if(fullCompare == 0)
{
String security1 = htmlLogLine1.getSecurity();
String security2 = htmlLogLine2.getSecurity();
if(security1 != null && security2 != null)
{
fullCompare = security1.compareTo(security2);
}
else if(security1 == null && security2 != null)
{
fullCompare = -1;
}
else if(security1 != null && security2 == null)
{
fullCompare = 1;
}
else
{
fullCompare = 0;
}
}
if(fullCompare == 0)
{
String scenario1 = htmlLogLine1.getScenario();
String scenario2 = htmlLogLine2.getScenario();
if(scenario1 != null && scenario2 != null)
{
fullCompare = scenario1.compareTo(scenario2);
}
else if(scenario1 == null && scenario2 != null)
{
fullCompare = -1;
}
else if(scenario1 != null)
{
fullCompare = 1;
}
else
{
fullCompare = 0;
}
}
if(fullCompare == 0)
{
Optional<Instant> timestamp1 = htmlLogLine1.getTimestamp();
Optional<Instant> timestamp2 = htmlLogLine2.getTimestamp();
if(timestamp1.isPresent() && timestamp2.isPresent())
{
fullCompare = timestamp1.get().compareTo(timestamp2.get());
}
else if(!timestamp1.isPresent() && timestamp2.isPresent())
{
fullCompare = -1;
}
else if(timestamp1.isPresent() && !timestamp2.isPresent())
{
fullCompare = 1;
}
else
{
fullCompare = 0;
}
}
return fullCompare;
}
};
The code seems very naive and has a lot of duplication. Any ideas how to make it concise?
Previous to this code, I had written the following code in compare method:
Comparator<HTMLLogLine > fullComparator = Comparator.comparing(HTMLLogLine ::getRequestId)
.thenComparing(HTMLLogLine ::getSecurity)
.thenComparing(HTMLLogLine ::getScenario);
fullCompare = fullComparator.compare(logLine1, logLine2);
However,to my surprise the code threw exception: Compare method does not follow the contract!
How can I make this comparator concise yet make it work?
So my issue here is getting this program to combine like terms, I've tried about 3 different ways of going about this and cannot figure it out. I can sort the array in somewhat the correct order but not get it to combine.
public static Node multiply(Node poly1, Node poly2) {
Node polynomial1 = poly1;
Node polynomial2 = poly2;
Node mult = poly2;
Node temp3 = null;
float cmb = 0;
int cmbExp = 0;
// the sum of the polynomials
Node temp = null;
float coeffProd;
int expSum;
if(poly1 == null)
return "Enter a Different Polynomial";
if(poly2 == null)
return "Enter a Different Polynomial";
// while loop if either polynomial runs out of terms. This avoids nullpointerexception
while (polynomial1 != null) {
while(polynomial2 != null){
coeffProd = polynomial1.term.coeff * polynomial2.term.coeff;
expSum = polynomial1.term.degree + polynomial2.term.degree;
temp = new Node(coeffProd, expSum, temp);
polynomial2 = polynomial2.next;
}
polynomial1 = polynomial1.next;
polynomial2 = mult;
}
// my attempt at combining
Node temp2 = temp.next;
while(temp != null){
while(temp2 != null){
if(temp.term.degree == temp2.term.degree){
cmb = temp.term.coeff + temp2.term.coeff;
cmbExp = temp.term.degree;
}
temp2 = temp2.next;
temp3 = new Node(cmb,cmbExp,temp3);
}
temp = temp.next;
}
// Reverses the Polynomial into the correct sequence
Node flip = temp;
Node ans = null;
while (flip != null)
{
if (flip.term.coeff == 0)
{
flip = flip.next;
continue;
}
else
{
ans = new Node(flip.term.coeff, flip.term.degree, ans);
flip = flip.next;
}
}
if (ans == null)
return null;
else
{
return ans;
}
}
Result:
32.0x^9 + 16.0x^8 + -12.0x^6 + 36.0x^5 + -16.0x^7 + -8.0x^6 + 6.0x^4 + -18.0x^3 + 16.0x^5 + 8.0x^4 + -6.0x^2 + 18.0x + 24.0x^4 + 12.0x^3 + -9.0x + 27.0
I need to combine these like terms.
The combining can be simplified if you iterate through the degrees. This also guarantees that the combined polynomial is created with the degrees in sequential order.
The following working code demonstrates one possible way to combine, and create the desired result.
public Node multiply(Node poly1, Node poly2) {
if (poly1 == null || poly2 == null)
return null;
Node polynomial1 = poly1;
Node polynomial2 = poly2;
Node allTerms = null;
float coeffProd;
int expSum;
int maxDegree = 0;
while (polynomial1 != null) {
while (polynomial2 != null) {
coeffProd = polynomial1.term.coeff * polynomial2.term.coeff;
expSum = polynomial1.term.degree + polynomial2.term.degree;
allTerms = new Node(coeffProd, expSum, allTerms);
if (expSum > maxDegree)
maxDegree = expSum;
polynomial2 = polynomial2.next;
}
polynomial1 = polynomial1.next;
polynomial2 = poly2;
}
/* Combine terms */
Node combinedNode = null;
for (int i = 0; i<= maxDegree; i++) {
Node temp = allTerms;
float fSum = 0;
while (temp != null) {
if (temp.term.degree == i)
fSum+=temp.term.coeff;
temp = temp.next;
}
if (fSum != 0)
combinedNode = new Node(fSum, i, combinedNode);
}
return combinedNode;
}
Currently I've written a method that adds two polynomials together. Poly1 and Poly2. The logic of the method is as follows, first it adds all matching degree terms from Poly1 and Poly2, then it adds all non-matching terms from Poly1, and finally adds all non-matching terms from Poly2. But because of this, the terms are out of order.
Polynomial answer = new Polynomial();
for (Node firstPoly = poly; firstPoly != null; firstPoly = firstPoly.next){
boolean polyAdded = false;
for (Node secondPoly = p.poly; secondPoly != null; secondPoly = secondPoly.next){
if (firstPoly.term.degree == secondPoly.term.degree){
answer = addToRear(answer, (firstPoly.term.coeff + secondPoly.term.coeff), firstPoly.term.degree, null);
if (answer.poly.term.coeff == 0){
answer.poly = null;
}
polyAdded = true;
}
}
if (polyAdded == false){
answer = addToRear(answer, firstPoly.term.coeff, firstPoly.term.degree, null);
if (answer.poly.term.coeff == 0){
answer.poly = null;
}
}
}
for (Node secondPoly = p.poly; secondPoly != null; secondPoly = secondPoly.next){
boolean match = false;
for (Node answerPoly = answer.poly; answerPoly != null; answerPoly = answerPoly.next){
if (secondPoly.term.degree == answerPoly.term.degree){
match = true;
break;
}
}
if (match == false){
answer = addToRear(answer, secondPoly.term.coeff, secondPoly.term.degree, null);
}
}
return answer;
//alt + shift + r
}
If this code outputs:
8.0x^4 + 4.0x^5 + 2.0x^3 + -1.0x + 12.0
the linked list is represented as such:
(coefficient, degree) // (12, 0) -> (-1, 1) -> (2, 3) -> (4, 5) -> (8, 4)
I want to now sort my answer polynomial by order of degree. The linked list should be represented like this:
(coefficient, degree) // (12, 0) -> (-1, 1) -> (2, 3) -> (8, 4) -> (4, 5)
EDIT: Found the solution on my own. Here is the sorting method I created:
private Polynomial sortByDegree(Polynomial p){
Node prev = p.poly;
Node current = p.poly.next;
while (current != null){
if (current.term.degree < prev.term.degree){
int temp = current.term.degree;
current.term.degree = prev.term.degree;
prev.term.degree = temp;
float temp2 = current.term.coeff;
current.term.coeff = prev.term.coeff;
prev.term.coeff = temp2;
prev = p.poly;
current = p.poly.next;
}
prev = prev.next;
current = current.next;
}
return p;
}
Thanks everyone!
Can suggest two approaches:
First. Fetch linked list into array list (stack), sort array list, rebuild as linked list.
Second. Use following algo:
void sortPoly(Polynomial answer) {
float lowerMargin = 0;
Node head = null, tail = null;
while (answer.poly != null) {
Node next = detouchMin(lowerMargin, answer);
lowerMargin = next.term.degree;
if (tail != null) {
tail.next = next;
tail = next;
else {
head = next;
tail = next;
}
}
answer.poly = head;
}
Node detouchMin(float lowerMargin, Polynomial answer) {
float min = Float.inf;
Node n = null;
Node t = answer.poly;
while (t != null) {
if ((t.term.degree > lowerMargin) && (t.term.degree < min)) {
n = t;
min = n.term.degree;
}
t = t.next;
}
if (n != null) {
Node t = answer.poly, prev = null;
while (t != null) {
if (t == n) {
if (prev != null)
answer.poly = t.next;
else
prev.next = t.next;
}
prev = t;
t = t.next;
}
}
return n;
}
Note: code not tested