简体   繁体   English

为什么不能从链接列表中删除重复项?

[英]Why can't I remove duplicates from linked list?

     /*
DEFINITIONS

 public class ListNode {
        int val;
        ListNode next;
        public ListNode(int x){
            val = x;
            next = null;
        }

    }


    public class LinkedList {
        ListNode head;
    }
    */


    public void removeDups(){
        ListNode head = this.head;
        if(head == null) return;

        HashSet<ListNode> set = new HashSet<>();
        set.add(head);
        while(head.next != null){
            if(set.contains(head.next)){
                head.next = head.next.next;
            } else{
                set.add(head.next);
                head = head.next;
            }
        }
    }

I am supposed to remove duplicates from an unsorted list, but it isn't working in Java. 我应该从一个未排序的列表中删除重复项,但是它在Java中不起作用。

1->2->4->9->9->4 should return 1->2->4->9 but it still returns 1->2->4->9->9->4 1->2->4->9->9->4应该返回1->2->4->9但仍返回1->2->4->9->9->4

What is the issue? 有什么问题 I am clearly inserting all nodes into a hashset and checking if it contains, but not sure what is happening? 我清楚地将所有节点插入到哈希集中,并检查它是否包含,但是不确定发生了什么?

You are removing duplicate nodes, not duplicate valued nodes in the code you posted. 您将在发布的代码中删除重复的节点,而不是重复的有价值的节点。

 /*
DEFINITIONS

public class ListNode {
    int val;
    ListNode next;
    public ListNode(int x){
        val = x;
        next = null;
    }

}


public class LinkedList {
    ListNode head;
}
*/


public void removeDups(){
    ListNode head = this.head;
    if(head == null) return;

    HashSet<Integer> set = new HashSet<Integer>();
    set.add(head.val);
    while(head.next != null){
        if(set.contains(head.next.val)){
            head.next = head.next.next;
        } else{
            set.add(head.next.val);
            head = head.next;
        }
    }
}

You are trying to check if set contains the ListNode , but you didn't override the equals method in ListNode class. 您正在尝试检查set包含ListNode ,但没有在ListNode类中重写equals方法。 Try overriding it, or add values to set, not the whole nodes, it will work since values are simple integers. 尝试覆盖它,或添加要设置的值而不是整个节点,因为值是简单的整数,所以它将起作用。

You should also override the hashCode method, as you should always do when overriding equals - thanks Andy Turner 您还应该覆盖hashCode方法,就像在覆盖equals时应该做的一样-谢谢Andy Turner

Just to add to v78's answer , which correctly describes the reason why it doesn't currently work: 只是添加到v78的答案中 ,该答案正确描述了当前无法正常工作的原因:

Alternatively, you could implement equals and hashCode in the ListNode class, which would allow the nodes to be considered equal if they have the same val : 另外,您可以在ListNode类中实现equalshashCode ,如果节点具有相同的val则可以将它们视为相等:

int hashCode() { return val; }

boolean equals(Object other) {
  if (other == this) return true;
  return (other instanceof ListNode)
      && val == ((ListNode) other).val;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM