I am trying to make a method that removes any value in a linked list that is larger than the input max. I have written out the code and it works for every case except when the first node is greater than the input max value, which then produces a NullPointerException. I was hoping that someone could help me out.
Here is the code I have so far:
public static void removeValuesLargerThanMax(SinglyLinkedList list, int max){
Node cur = list.head;
Node prev = null;
while(cur != null){
if(cur.data > max){
prev.next = cur.next;
cur = cur.next;
continue;
}
prev = cur;
cur = cur.next;
}
}