检查链表的元素是否存在

时间:2013-12-02 15:12:07

标签: java linked-list nodes

我正在尝试确定该值是否是链接中的成员。如果它是一个成员,它将返回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;
}

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

6 个答案:

答案 0 :(得分:3)

只需传递根节点并遍历您的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;
}

答案 1 :(得分:2)

没有递归就行了:

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

您可以使用此代码..

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

答案 4 :(得分:0)

{
    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();
            }
        }
    }
}

答案 5 :(得分:-1)

这是因为您没有在循环结束时递增节点值。还要进行空检查以检查结束。

        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;
}
    }