CSC300: Common mistakes: Does this work? [9/10] Previous pageContentsNext page

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
  public int numFives () {
    if (first.next == null) return 0;
    Node x = first;
    int result = 0;
    while (x.next != null) {
      if (x.item != 5.0) x = x.next;
      if (x.item == 5.0) {
        result = result + 1;
        x = x.next; 
      }
    }
    if (x.next == null && x.item == 5.0) {
      result = result + 1;
    }
    return result;
  }

Previous pageContentsNext page