I’m trying to create the indexOf solution for Data structures. Whenever I run my code I get the return value of -1, despite trying to print one of the items in my list i.e. 10,20,30.
Below is my LinkedList class, can anyone see what I’m doing wrong?
package com.codewithmosh;
public class LinkedList {
private class Node {
private int value;
private Node next;
public Node(int value) {
this.value = value;
}
}
private Node first;
private Node last;
public void addLast(int item) {
}
public void addFirst(int item) {
}
private boolean isEmpty() {
return first == null;
}
public int indexOf(int item) {
int index = 0;
var current = first;
while (current != null) {
if (current.value == item) return index;
current = current.next;
index++;
}
return -1;
}
}
Thanks