简体   繁体   中英

LinkedList: Removing the duplicates

I know this is a duplicate problem but my question is different.
Help me to understand few lines of this code.
It removes duplicate nodes from a single linked list.

public class DeleteDuplicates {

    static void deleteDups(LinkedListNode n) {
        Hashtable table = new Hashtable();
        LinkedListNode previous = null;
        while(n!=null) {
            if(table.containsKey(n.data)) {
                previous.next = n.next;
            } else {
                table.put(n.data, true);
                previous = n;
            }
            System.out.println(n.next.data);
            n = n.next;
        }
    }

    public static void main(String[] args) {
        LinkedListNode node_1 = new LinkedListNode("first");        
        LinkedListNode node_2 = new LinkedListNode("second");
        node_1.next = node_2;
        LinkedListNode node_3 = new LinkedListNode("third");
        node_2.next = node_3;
        LinkedListNode node_4 = new LinkedListNode("second");
        node_3.next = node_4;

        LinkedListNode  current = node_1;
        deleteDups(current);
        while (current != null) {
            System.out.println(current.data);
            current = current.next;
        }

    }

}

Questions I have are:

  1. How come LinkedList n is skipping the duplicate node? I didn't understand the use of previous node and how it is helping in skipping the duplicate node.
  2. How important is the use of Hashtable ? Can I use any other collection for example HashSet ?

You already have good answers to your question 2, so I'll just concentrate on question 1 (really you should only ask 1 question in each post, by the way). This is how the removal of the duplicate works:

In each iteration through your loop, previous holds a reference to the node in the list before the node n . So, when n is set to your node_4 , previous is set to node_3 . Therefore, previous.next = n.next is equivalent to node_3.next = node_4.next , which because you don't set a value for node_4.next is equivalent to node_3.next = null , which has the effect of making node_3 the last node in the list, thus removing node_4 from the list.

If, instead of node_4 being the duplicate, it was node_3 that was duplicated, then previous would be node_2 , n would be node_3 and the change made would be equivalent to node_2.next = node_3.next , which is to say node_2.next = node_4 or in plain English, "make the next node after node_2 be node_4", effectively removing node_3 from the list.

1) LinkedList is not skipping the duplicate node, it is being mapped out - next is pointed to the entry after the duplicate.
2) Think LinkedList allows duplicates, but Hashtable does not -> make your conclusion from this

You can use any data structure you like for detecting duplicates.

From an implementation point of view, hashes are nice because they take (amortized) constant time to determine whether or not a particular element is a duplicate.

From an API point of view, the Collection.Set interface is nice because it guarantees no duplicate items.

So your idea of using a HashSet seems very intuitive, especially because you're only interested in duplicate keys, without regard for the actual node object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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