I am trying to count the number of time a particular integer occurs within my Linked List. However I am running into an infinite loop. I try to print variables to see where the code reaches but nothing prints. I was wondering if someone could be an extra pair of eyes for me.
My LinkedListNode class is simply:
public class LinkedListNode {
int data;
public LinkedListNode next;
// constructor
public LinkedListNode(int newData) {
this.next = null;
this.data = newData;
}
}
My code:
public static int countInt(LinkedListNode head, int number) {
int count = 0;
while (head.next != null) {
if (head.data == number) {
count++;
//System.out.println(count);
head = head.next;
//System.out.println(head.data);
}
}
return count;
}