Miguel Barra
Miguel Barra

Reputation: 11

Remove duplicates from a sorted linked list in a efficient way

I'm on HackerRank and I need to remove duplicate items from a sorted linked list. I passed all the cases except for two of them which the input is something like: 10001102034 So my program takes to seconds to complete and exceed the time. How can I do my code more efficiently, I heard about using square root but I don't know how to use it. Any guide is appreciate. Here is my code.

private static Node removeDuplicates(Node head) {
        /* Another reference to head */
        Node current = head;
        Node next;

        /* Traverse list till the last node */
        while (current != null && current.next != null) {
            if (current.data == current.next.data) {
                next = current.next.next;
                if (next == null) {
                    current.next = null;
                    break;
                }
                current.next = next;
            } else {
                current = current.next;
            }
        }
        return head;
    }

Again. It works but takes too much times with longer numbers.

Upvotes: 0

Views: 119

Answers (2)

Nowhere Man
Nowhere Man

Reputation: 19565

You should replace condition if (current.data == current.next.data) with while loop and use break 'label':

out:
while (current != null && current.next != null) {
    while (current.data == current.next.data) {
        next = current.next.next;
        if (next == null) {
            current.next = null;
            break out;
        }
        current.next = next;
    } 
    current = current.next;
}

Upvotes: 2

Loai
Loai

Reputation: 40

You can't use the square root because when u want to remove duplicates from a list you have to check all the list . The square root technique is used for searching in a sorted list. But for your question if you can improve the runtime on that your code in O(n^2) but if you change your code to use hashtable you can make it O(n).

import java.util.HashSet;

public class removeDuplicates
{ static class node
{ int val; node next;

    public node(int val)  
    { 
        this.val = val; 
    } 
} 

/* Function to remove duplicates from a 
   unsorted linked list */
static void removeDuplicate(node head)  
{ 
    // Hash to store seen values 
    HashSet<Integer> hs = new HashSet<>(); 

    /* 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; 
    } 

} 

/* Function to print nodes in a given linked list */
static void printList(node head)  
{ 
    while (head != null)  
    { 
        System.out.print(head.val + " "); 
        head = head.next; 
    } 
} 

I hope this will help you.

Upvotes: 0

Related Questions