The task is to delete the nth node of a doubly-linked list. I drew it out, and I don't see the nullpointer error that's tripping me up.
I've created an if-statement to handle the edge case of an empty list. I created a while loop to traverse the linked list otherwise. I get there error when I'm attempting to set the node links. Details below.
public double delete (int k) {
int i = 0;
Node temp = first;
double retVal;
if (k < 0 || k >= N) throw new IllegalArgumentException ();
if (temp == null)
return 0;
while (temp != null) {
if (i == k - 1) {
temp.next = temp.next.next;
temp.next.next.prev = temp;
temp.next.next = null;
temp.next.prev = null;
return temp.next.item;
} else {
i++;
temp = temp.next;
}
}
return 0;
I was expecting to delete and return the node to be deleted. Instead I got a nullpointerexception at the line commented with "//Issue".