I'm stuck on this exercise where I have to implement a reverse method for a doubly linked list, which reverses the entire list. I'm getting a NullPointerException in my for loop and I'm not sure how to solve it as in the exercise it is stated that I am not supposed to create new IntNode instances. I keep getting the exception even if I use an if-else statement if(current.next != null) ... Any help would be much appreciated!
void reverse() {
IntNode temp1;
IntNode temp2;
// TODO: Vervollständigen Sie die Methode wie in der Aufgabenstellung gefordert.
this.last.next = this.last.prev;
this.last.prev = null;
for (IntNode current = this.last.next; current != this.first; current = current.prev) {
temp1 = current.next;
current.next = current.prev;
current.prev = temp1;
}
this.first.prev = this.first.next;
this.first.next = null;
temp2 = this.first;
this.first = this.last;
this.last = temp2;
}