Java 检查链表元素是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20331535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:07:51  来源:igfitidea点击:

Check if element of linked list exists

javalinked-listnodes

提问by user2779065

I am trying to determine if the value is a member in the link. if it is a member, it will return true otherwise it returns false. I try to do this

我试图确定该值是否是链接中的成员。如果是成员,则返回true,否则返回false。我尝试这样做

public boolean member(Integer val){
    if(node.val.equals(val) || node.next.val.equals(val)){
        System.out.println("true");
        return true;
    }
    return false;
}

However, it only check the value of the head and value of the next of the head. What would be other way to check the value of all node?

但是,它只检查头部的值和头部的下一个值。检查所有节点的值的其他方法是什么?

采纳答案by But I'm Not A Wrapper Class

Just pass the root node and loop through your linkedList:

只需传递根节点并循环遍历您的链表:

public boolean member(Node root, Integer val){
    Node currentNode = root;//keep track of current node
    while(currentNode != null){
        //switched currentNode.val and val to avoid null pointer exceptions
        if(val.equals(currentNode.val)){
        //if you're looking for nulls...still with the original
        //if(currentNode.val.equals(val)){
            System.out.println("true");
            return true;
        }
        currentNode = currentNode.next();//update current node
    }
    return false;
}

回答by user2779065

you can use this code..

你可以使用这个代码..

boolean exists = linkedList.contains("element");    
System.out.println("element exists in LinkedList ? : " + exists);

回答by Blub

Ok without recursion:

好的没有递归:

public boolean member(Integer val){
  Node current = node;
  while (current != null) {
    if (current.val.equals(val)) {
      return true;
    } else {
      current = current.next;
    }
  }
  return false;
}

回答by user1801279

This is because you are not incrementing the node value at the end of the loop . Also do a null check to check the end .

这是因为您没有在循环结束时增加节点值。还要做一个空检查来检查结束。

        public boolean member(Integer val){
            if(node.next!=null){
            if(node.val.equals(val) || node.next.val.equals(val)){
                System.out.println("true");
                node = node.next; // incrementing the node value 
                return true;
            }
            return false;
        }
    }
    else {
    if( node.val.equals(val)){
    return true;
    }
else{
return false;
}
    }

回答by Dhanveer thakur

{
    if (s.length() == 0) {
        rvFriendsList.setVisibility(View.VISIBLE);
        rvSearch_Friends.setVisibility(View.GONE);
        FriendsType ="actual";
    }
    if (s.length() >= 1) {
        FriendsType ="search";
        rvFriendsList.setVisibility(View.GONE);
        rvSearch_Friends.setVisibility(View.VISIBLE);
        rvSearch_Friends.bringToFront();
        btnCancel.setTextColor(getResources().getColor(R.color.red));
        String searchString = s.toString().toLowerCase(Locale.getDefault());
        Log.d("@w2w2w2w", "" + searchString);
        int realtext = searchString.length();
        linkedList_SearchUser.clear();
        for (int i = 0; i < linkList_GetAllContacts.size(); i++) {
            String currentString = linkList_GetAllContacts.get(i).get("first_name").toString();
            if (realtext <= currentString.length()) {
                if (searchString.equalsIgnoreCase(currentString.substring(0, realtext))) {
                    linkedList_SearchUser.add(linkList_GetAllContacts.get(i));
                    hype1 = new Custom_Adapter_GetAllContacts((Activity) context, linkedList_SearchUser);
                    LinearLayoutManager llm = new LinearLayoutManager(context);
                    llm.setOrientation(LinearLayoutManager.VERTICAL);
                    rvSearch_Friends.setLayoutManager(llm);
                    rvSearch_Friends.setAdapter(hype1);
                }
            } else {
                hype1.notifyDataSetChanged();
            }
        }
    }
}

回答by MarioWSU

This solution uses a Boolean function to determine if element exists in linked list.

此解决方案使用布尔函数来确定元素是否存在于链表中。

    public boolean doesValExist(int value){
    Node traveler = head;
    while(traveler != null){
        if(traveler.getElement() == value){
            return true;
        }
        traveler = traveler.getNext();
    }
    return false;
}