Remove duplicates from an unsorted linked list

前端 未结 19 976
梦如初夏
梦如初夏 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:29
    /**
    *
    * Remove duplicates from an unsorted linked list.
    */
    public class RemoveDuplicates {
        public static void main(String[] args) {
            LinkedList<String> list = new LinkedList<String>();
            list.add("Apple");
            list.add("Grape");
            list.add("Apple");
            HashSet<String> set = removeDuplicatesFromList(list);
            System.out.println("Removed duplicates" + set);
        }
        public static HashSet<String> removeDuplicatesFromList(LinkedList<String> list){
            HashSet<String> set = new LinkedHashSet<String>();
            set.addAll(list);
            return set;
        }
    }
    
    0 讨论(0)
  • 2020-12-09 07:30

    Below code implements this without needing any temporary buffer. It starts with comparing first and second nodes, if not a match, it adds char at first node to second node then proceeds comparing all chars in second node to char at third node and so on. After comperison is complete, before leaving the node it clears everything that is added and restores its old value which resides at node.val.char(0)

    F > FO > FOL > (match found, node.next = node.next.next) > (again match, discard it) > FOLW > ....

    public void onlyUnique(){
            Node node = first;
            while(node.next != null){
                for(int i = 0 ; i < node.val.length(); i++){
                    if(node.val.charAt(i) == node.next.val.charAt(0)){
                        node.next = node.next.next;
                    }else{
                        if(node.next.next != null){ //no need to copy everything to the last element
                            node.next.val = node.next.val + node.val;
                        }
                        node.val = node.val.charAt(0)+ "";
                    }
                }
                node = node.next;
            }
        }
    
    0 讨论(0)
  • 2020-12-09 07:30

    1.Fully Dynamic Approach 2.Remove Duplicates from LinkedList 3.LinkedList Dynamic Object based creation Thank you

    import java.util.Scanner;
    class Node{
        int data;
        Node next;
        public Node(int data)
        {
            this.data=data;
            this.next=null;
        }
    }
    class Solution
    {
        public static Node insert(Node head,int data)
        {
        Node p=new Node(data);
        if(head==null)
        {
            head=p;
        }
        else if(head.next==null)
        {
            head.next=p;
        }
        else
        {
            Node start=head;
            while(start.next!=null)
            {
                start=start.next;   
            }
            start.next=p;
            return head;
            //System.out.println();
        }
        return head;    
        }
        public static void display(Node head)
        {
            Node start=head;
            while(start!=null)
            {
                System.out.print(start.data+" ");
                start=start.next;
            }
    
        }
        public static Node remove_duplicates(Node head)
        {
           if(head==null||head.next==null)
           {
               return head;
           }
           Node prev=head;
           Node p=head.next;
    
           while(p!=null)
           {
               if(p.data==prev.data)
               {
                prev.next=p.next;
                p=p.next;              
               }
               else{
                   prev=p;
                   p=p.next;   
               }
           }
           return head;
        }
        public static void main(String args[])
        {
            Scanner sc=new Scanner(System.in);
            Node head=null;
            int T=sc.nextInt();
            while(T-->0)
            {
                int ele=sc.nextInt();
                head=insert(head,ele);
            }
            head=remove_duplicates(head);
            display(head);  
        }
    }
    

    input: 5 1 1 2 3 3 output: 1 2 3

    0 讨论(0)
  • 2020-12-09 07:33
    public static void main(String[] args) {
    
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(2);
        linkedList.add(3);
        linkedList.add(4);
        linkedList.add(5);
        linkedList.add(6);
        deleteElement(linkedList);
        System.out.println(linkedList);
    }
    
    private static void deleteElement(LinkedList<Integer> linkedList) {
    
        Set s = new HashSet<Integer>();
    
        s.addAll(linkedList);
        linkedList.clear();
        linkedList.addAll(s);
    
    }
    
    0 讨论(0)
  • 2020-12-09 07:34
    I think you can just use one iterator current to finish this problem 
    
    public void compress(){
        ListNode current = front;
        HashSet<Integer> set = new HashSet<Integer>();
        set.add(current.data);
        while(current.next != null){
           if(set.contains(current.next.data)){
              current.next = current.next.next;         }
             else{
                set.add(current.next.data);
                current = current.next;
             }
          }
    

    }

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

    The first problem is that

    LinkedListNode head=new LinkedListNode(list.getFirst());
    

    does not actually initialize head with the contents of list. list.getFirst() simply returns the Integer 1, and head contains 1 as its only element. You would have to initialize head by looping through list in order to get all of the elements.

    In addition, although

    Task.deleteDups(head)
    

    modifies head, this leaves list completely unchanged—there is no reason why the changes to head should propagate to list. Therefore, to check your method, you would have to loop down head and print out each element, rather than printing out list again.

    0 讨论(0)
提交回复
热议问题