I have to check if an element is present in a linkedlist or not? I wrote the following code but it is not returning the right output.I m unable to find what I did wrong.Can anyone tell what I did wrong in the following code? The expected output is false and true but the output I am getting is false and false.
package BasicList1;
import java.util.Vector;
public class BasicList1 implements ListInterface{
static String[] testcase1 = {"3","1","2","6","7","4","5"};
public static void main (String[] args){
BasicList1 testInstance = new BasicList1();
ListNode head = new ListNode(testcase1[0]);
ListNode node = head;
System.out.println(testInstance.elements(head));
System.out.println(testInstance.hasElement(head, "9"));
System.out.println(testInstance.hasElement(head,"4"));
}
public BasicList1 getBasicList(String data){
return this;
}
//write your code here
public Vector<String> elements(ListNode head){
ListNode temp=head;
Vector v=new Vector();
while(temp!=null){
v.addElement(temp);
temp=temp.next;
}
return v;
}
@Override
public boolean hasElement(ListNode head, String data) {
ListNode temp=head;
while(temp!=null){
if(temp.data.equals(data)){
return true;
}
temp=temp.next;
}
return false;
}