简体   繁体   English

以有效的方式从排序的链表中删除重复项

[英]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.我在 HackerRank 上,我需要从排序的链接列表中删除重复的项目。 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.我通过了所有案例,除了其中两个输入类似于:10001102034 所以我的程序需要几秒钟才能完成并超过时间。 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.它有效,但数字较长时需要太多时间。

You should replace condition if (current.data == current.next.data) with while loop and use break 'label' :您应该用while循环替换条件if (current.data == current.next.data)并使用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;
}

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).但是对于您的问题,如果您可以在 O(n^2) 中改进您的代码的运行时间,但是如果您将代码更改为使用哈希表,则可以将其设为 O(n)。

import java.util.HashSet;导入 java.util.HashSet;

public class removeDuplicates公共 class 删除重复项
{ static class node { static class 节点
{ int val; { 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.我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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