Removing a range of values from a linked list -
i trying remove node linked list if value falls within range (greater or equal low , less or equal high). code removes first value found within range.
public void removedata(e low, e high) { node previousnode = root; node deletenode = previousnode.getnext(); while (deletenode != null) { if (deletenode.getvalue().compareto(low) >= 0 && deletenode.getvalue().compareto(high) <= 0) { previousnode.setnext(deletenode.getnext()); } previousnode = previousnode.getnext(); deletenode = deletenode.getnext(); } }
in code need is:
public void removedata(e low, e high) { node previousnode = root; node deletenode = previousnode.getnext(); while (deletenode != null) { if (deletenode.getvalue().compareto(low) >= 0 && deletenode.getvalue().compareto(high) <= 0) { previousnode.setnext(deletenode.getnext()); }else{ previousnode = previousnode.getnext(); } deletenode = deletenode.getnext(); }
}
because if 1 node deleted previous node doesn't need incremented deletenode has to. , check deletenode.
hope may you. thanks!!