删除LinkedList重复项

时间:2016-11-11 10:37:06

标签: java

我想删除链表中的重复项目,我想在列表中保留一个唯一的项目,任何想法怎么做? 注意:Hashset在此作业中是禁止的

此项目中使用的所有数据结构必须由学生实施。 严禁使用Java集合或任何其他库。

class Node<T> {
public T data;
public Node<T> next;

public Node (T val) {
    data = val;
    next = null;
}}

以下是LinkeList类

public class LinkedList<T> {
private Node<T> head;
private Node<T> current;

public LinkedList () {
    head = current = null;
}

public boolean empty () {
    return head == null;
}

public boolean last () {
    return current.next == null;
}

public boolean full () {
    return false;
}

public void findFirst () {
    current = head;
}
public void findNext () {
    current = current.next;
}

public T retrieve () {
    return current.data;
}

public void update (T val) {
    current.data = val;
}

public void insert (T val) {
    Node<T> tmp;
    if (empty()) {
        current = head = new Node<T> (val);
    }
    else {
        tmp = current.next;
        current.next = new Node<T> (val);
        current = current.next;
        current.next = tmp;
    }
}

public void remove () {
    if (current == head) {
        head = head.next;
    }
    else {
        Node<T> tmp = head;
        while (tmp.next != current)
            tmp = tmp.next;
        tmp.next = current.next;
    }
    if (current.next == null)
        current = head;
    else
        current = current.next;
}}

LinkedList Elements

    public static void main(String[] args) {

    LinkedList<Integer> list=new LinkedList<>();

    list.insert(1);
    list.insert(2);
    list.insert(1);
    list.insert(3);
    list.insert(1);
    list.insert(2);
    list.insert(4);
    list.insert(3);
    list.insert(5);
    list.insert(4);
    list.insert(7);
    list.insert(1);
    list.insert(6);
    list.insert(5);
    list.insert(6);
    list.insert(1);
    list.insert(2);}

我一直坚持这个请求帮助

感谢

4 个答案:

答案 0 :(得分:3)

尝试添加contain()方法(请注意,此代码的灵感来自LinkedList代码来源,您可以检查LinkedList.contains(Object o)

public boolean contain(T o) {
    if (o == null) { // assuming that your list accept to add null object
        for (Node<T> x = first; x != null; x = x.next) {
            if (x.data == null)
                return true;
        }
    } else {
        for (Node<T> x = first; x != null; x = x.next) {
            if (o.equals(x.data))
                return true;
        }
    }
    return false;
}

编辑insert()方法以在添加对象之前检查对象是否已存在

public void insert (T val) {
    Node<T> tmp;
    if (empty()) {
        current = head = new Node<T> (val);
    }
    else if(!contain(val)) { // the object val will be added only if it not exist in the list
        tmp = current.next;
        current.next = new Node<T> (val);
        current = current.next;
        current.next = tmp;
    }
}

答案 1 :(得分:1)

1)您需要对可以使用“mergesort Linked List”的链表进行排序,您可以在网上轻松找到它,然后在遍历列表时删除一次。复杂性将是O(nlogn)。 链接:http://algorithms.tutorialhorizon.com/merge-sort-in-a-linked-list/

OR

2)检查开始时的每个元素,并在遍历每个节点的整个LL时删除重复节点。  复杂性:O(n ^ 2)

答案 2 :(得分:0)

如果非空,则使用hashset并将root插入其中。 现在开始遍历,如果下一个元素在hashset中,则将其删除,否则将其插入到hashset中并继续遍历。

如果禁止使用hashset,则对其进行排序,如果next元素等于当前删除它,则继续遍历。

请参阅here

答案 3 :(得分:0)

您可以尝试这样的事情:

List<String> list = Arrays.asList("one", "one", "one", "two");
List<String> unique=list.stream().distinct().collect(Collectors.toList());
相关问题