Remove duplicates from an unsorted linked list

前端 未结 19 977
梦如初夏
梦如初夏 2020-12-09 06:54
import java.util.*;
/*
 *  Remove duplicates from an unsorted linked list
 */
public class LinkedListNode {  
    public int data;  
    public LinkedListNode next;          


        
相关标签:
19条回答
  • 2020-12-09 07:36

    You can use the following java method to remove duplicates:

    1) With complexity of O(n^2)

    public void removeDuplicate(Node head) {
        Node temp = head;
        Node duplicate = null;                //will point to duplicate node
        Node prev = null;                     //prev node to duplicate node
        while (temp != null) {                //iterate through all nodes       
            Node curr = temp;
            while (curr != null) {                     //compare each one by one
                /*in case of duplicate skip the node by using prev node*/
                if (curr.getData() == temp.getData() && temp != curr) {        
                    duplicate = curr;
                    prev.setNext(duplicate.getNext());
                }
                prev = curr;
                curr = curr.getNext();
            }
            temp = temp.getNext();
        }
    }
    

    Input:1=>2=>3=>5=>5=>1=>3=>

    Output:1=>2=>3=>5=>

    2)With complexity of O(n) using hash table.

    public void removeDuplicateUsingMap(Node head) {
        Node temp = head;
        Map<Integer, Boolean> hash_map = new HashMap<Integer, Boolean>(); //create a hash map
        while (temp != null) {  
            if (hash_map.get(temp.getData()) == null) {  //if key is not set then set is false
                hash_map.put(temp.getData(), false);
            } else {                                   //if key is already there,then delete the node
                deleteNode(temp,head);
            }
            temp = temp.getNext();
        }
    
    }
    
    public void deleteNode(Node n, Node start) {
            Node temp = start;
            if (n == start) {
                start = null;
            } else {
                while (temp.getNext() != n) {
                    temp = temp.getNext();
                }
    
                temp.setNext(n.getNext());
    
            }
        }
    

    Input:1=>2=>3=>5=>5=>1=>3=>

    Output:1=>2=>3=>5=>

    0 讨论(0)
  • 2020-12-09 07:41

    Try This.Its working. // Removing Duplicates in Linked List

    import java.io.*;
    import java.util.*;
    import java.text.*;
    class LinkedListNode{
        int data;
        LinkedListNode next=null;
    
        public LinkedListNode(int d){
            data=d;
        }
        void appendToTail(int d){
            LinkedListNode newnode = new LinkedListNode(d);
            LinkedListNode n=this;
            while(n.next!=null){
                n=n.next;
            }
            n.next=newnode;
        }
    
        void print(){
            LinkedListNode n=this;
            System.out.print("Linked List: ");
            while(n.next!=null){
                System.out.print(n.data+" -> ");
                n=n.next;
            }
            System.out.println(n.data);
        }
    }
    class LinkedList2_0
    {
        public static void deletenode2(LinkedListNode head,int d){
            LinkedListNode n=head;
            // If It's head node
            if(n.data==d){
                head=n.next;
            }
            //If its other
            while(n.next!=null){
                if(n.next.data==d){
                    n.next=n.next.next;
                }
                n=n.next;
            }
        }
    
        public static void removeDuplicateWithBuffer(LinkedListNode head){
            LinkedListNode n=head;
            LinkedListNode prev=null;
            Hashtable<Integer, Boolean> table = new Hashtable<Integer, Boolean>();
            while(n!=null){
                if(table.containsKey(n.data)){
                    prev.next=n.next;
                }
                else{
                    table.put(n.data,true);
                    prev=n;
                }
                n=n.next;
            }
        }
        public static void removeDuplicateWithoutBuffer(LinkedListNode head){
            LinkedListNode currentNode=head;
            while(currentNode!=null){
                LinkedListNode runner=currentNode;
                while(runner.next!=null){
                    if(runner.next.data==currentNode.data){
                        runner.next=runner.next.next;
                    }
                    else
                        runner=runner.next;
                }
                currentNode=currentNode.next;
            }
        }
        public static void main(String[] args) throws java.lang.Exception {
            LinkedListNode head=new LinkedListNode(1);
            head.appendToTail(1);
            head.appendToTail(3);
            head.appendToTail(2);
            head.appendToTail(3);
            head.appendToTail(4);
            head.appendToTail(5);
            head.print();
    
            System.out.print("After Delete: ");
            deletenode2(head,4);
            head.print();
    
            //System.out.print("After Removing Duplicates(with buffer): ");
            //removeDuplicateWithBuffer(head);
            //head.print();
    
            System.out.print("After Removing Duplicates(Without buffer): ");
            removeDuplicateWithoutBuffer(head);
            head.print();
    
        }
    }
    
    0 讨论(0)
  • 2020-12-09 07:43

    This is my Java version

    //  Remove duplicate from a sorted linked list
    public void removeDuplicates() {
    
            Node current = head;
            Node next = null;
            /* Traverse list till the last node */
            while (current != null) {
                next = current;
    
                /*
                 * Compare current node with the next node and keep on deleting them until it
                 * matches the current node data
                 */
                while (next != null && current.getValue() == next.getValue()) {
    
                    next = next.getNext();
    
                }
    
                /*
                 * Set current node next to the next different element denoted by temp
                 */
                current.setNext(next);
                current = current.getNext();
            }
        }
    
    0 讨论(0)
  • 2020-12-09 07:46

    Here's two ways of doing this in java. the method used above works in O(n) but requires additional space. Where as the second version runs in O(n^2) but requires no additional space.

    import java.util.Hashtable;
    
    public class LinkedList {
    LinkedListNode head;
    
    public static void main(String args[]){
        LinkedList list = new LinkedList();
        list.addNode(1);
        list.addNode(1);
        list.addNode(1);
        list.addNode(2);
        list.addNode(3);
        list.addNode(2);
    
        list.print();
        list.deleteDupsNoStorage(list.head);
        System.out.println();
        list.print();
    }
    
    public void print(){
        LinkedListNode n = head;
        while(n!=null){
            System.out.print(n.data +" ");
            n = n.next;
        }
    }
    
    public void deleteDups(LinkedListNode n){
        Hashtable<Integer, Boolean> table = new Hashtable<Integer, Boolean>();
        LinkedListNode prev = null;
    
        while(n !=null){
            if(table.containsKey(new Integer(n.data))){
                prev.next = n.next;     //skip the previously stored references next node
            }else{
                table.put(new Integer(n.data) , true);
                prev = n;       //stores a reference to n
            }
    
            n = n.next;
        }
    }
    
    public void deleteDupsNoStorage(LinkedListNode n){
        LinkedListNode current = n;
    
        while(current!=null){
            LinkedListNode runner = current;
            while(runner.next!=null){
                if(runner.next.data == current.data){
                    runner.next = runner.next.next;
                }else{
                    runner = runner.next;
                }
            }
            current = current.next;
        }
    
    }
    
    public void addNode(int d){
        LinkedListNode n = new LinkedListNode(d);
        if(this.head==null){
            this.head = n;
        }else{
            n.next = head;
            head = n;
        }
    }
    
    private class LinkedListNode{
        LinkedListNode next;
        int data;
    
        public LinkedListNode(int d){
            this.data = d;
        }
    }
    }
    
    0 讨论(0)
  • 2020-12-09 07:48

    here are a couple other solutions (slightly different from Cracking coding inerview, easier to read IMO).

    public void deleteDupes(Node head) {
    
    Node current = head;
    while (current != null) {
        Node next = current.next;
        while (next != null) {
            if (current.data == next.data) {
                current.next = next.next;
                break;
            }
    
            next = next.next;
        }
    
        current = current.next;
    }
    

    }

    public void deleteDupes(Node head) {
    Node current = head;
    while (current != null) {
        Node next = current.next;
        while (next != null) {
            if (current.data == next.data) {
                current.next = next.next;
                current = current.next;
                next = current.next;
            } else {
                next = next.next;
            }
        }
    
        current = current.next;
    }
    

    }

    0 讨论(0)
  • 2020-12-09 07:49

    Try this it is working for delete the duplicate elements from your linkedList

    package com.loknath.lab;
    
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.LinkedList;
    import java.util.Set;
    
    public class Task {
        public static void main(String[] args) {
    
            LinkedList<Integer> list = new LinkedList<Integer>();
            list.addLast(1);
            list.addLast(2);
            list.addLast(3);
            list.addLast(3);
            list.addLast(3);
            list.addLast(4);
            list.addLast(4);
            deleteDups(list);
            System.out.println(list);
        }
    
        public static void deleteDups(LinkedList<Integer> list) {
            Set s = new HashSet<Integer>();
            s.addAll(list);
            list.clear();
            list.addAll(s);
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题