Search code examples
javadata-structureslinked-listtraversalsingly-linked-list

Unable to delete an element from a linked list?


I am just practicing my data structures and trying to make a method to delete duplicates from a singly linked list. This is what I have:

        void removeDup() {
            Node temp = head;
            Node cur = null;
            String s = "";

            while(temp!=null) {
                cur = temp;

                if(!s.contains(temp.data + "")) {
                    s += temp.data + "";
                }
                else {
                    cur.next = temp.next;
                }

                temp = temp.next;
            }
        }

Printing the linked list after executing this method shows no changes. I believe this is because I am not properly linking the previous link to the current link's.next value, but everything looks correct to me. I debug it and it appears to delete the node correctly but the duplicate node still appears when I print out the linked list afterwards. Suggestions?


Solution

  • The code is copied from https://www.geeksforgeeks.org/remove-duplicates-from-an-unsorted-linked-list/:

    Method 1 - brute force,find all pairs of two nodes to see if they have same values, not sure if calling System.gc() is a good idea:

    /* Function to remove duplicates from an 
       unsorted linked list */
    void remove_duplicates() { 
        Node ptr1 = null, ptr2 = null, dup = null; 
        ptr1 = head; 
    
        /* Pick elements one by one */
        while (ptr1 != null && ptr1.next != null) { 
            ptr2 = ptr1; 
    
            /* Compare the picked element with rest 
                of the elements */
            while (ptr2.next != null) { 
    
                /* If duplicate then delete it */
                if (ptr1.data == ptr2.next.data) { 
    
                    /* sequence of steps is important here */
                    dup = ptr2.next; 
                    ptr2.next = ptr2.next.next; 
                    System.gc(); 
                } else /* This is tricky */ { 
                    ptr2 = ptr2.next; 
                } 
            } 
            ptr1 = ptr1.next; 
        } 
    }
    

    Method 2 - using hashset to help detect duplicate, I personally like this method better:

     /* Function to remove duplicates from a 
           unsorted linked list */
        static void removeDuplicate(node head)  
        { 
            // Hash to store seen values, changed a little to compile for Java 8
            HashSet<Integer> hs = new HashSet<Integer>(); 
    
            /* Pick elements one by one */
            node current = head; 
            node prev = null; 
            while (current != null)  
            { 
                int curval = current.val; 
    
                 // If current value is seen before 
                if (hs.contains(curval)) { 
                    prev.next = current.next; 
                } else { 
                    hs.add(curval); 
                    prev = current; 
                } 
                current = current.next; 
            } 
    
        }