簡體   English   中英

無法從鏈接列表中刪除元素?

[英]Unable to delete an element from a linked list?

我只是在練習我的數據結構,並嘗試創建一種方法來從單鏈列表中刪除重復項。 這就是我所擁有的:

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

執行此方法后,打印鏈接列表不會顯示任何更改。 我相信這是因為我沒有正確地將上一個鏈接鏈接到當前鏈接的.next值,但是一切對我來說都是正確的。 我調試了它,它似乎可以正確刪除該節點,但是當我以后打印出鏈接列表時,仍然出現重復的節點。 建議?

該代碼是從https://www.geeksforgeeks.org/remove-duplicates-from-an-unsorted-linked-list/復制的:

方法1-蠻力,找到兩個節點的所有對,看看它們是否具有相同的值,不確定是否調用System.gc()是一個好主意:

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

方法2-使用哈希集幫助檢測重復項,我個人更喜歡此方法:

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

    } 

首先,我認為您選擇將所有先前的內容保存在單個字符串中可能是一個壞主意。

例如,如果您使用{x,y,xy}填充列表。 第三項將被檢測為重復項。 結合簡單的替代方法。
將先前的值保存在某個collection /中,對於每個元素,檢查是否還有其他等效項。 整理所有東西,然后檢查人們的鄰居。

您將cur設置為temp; 在循環的頂部,因此執行cur.next = temp.next; 以后什么都不做。 不要在循環的頂部將cur設置為temp,或者在之后將其更改。

cur.next = temp.next不會更改任何內容。 使用例如Java 8:

new LinkedList<>(Arrays.asList(1,2,1,3)).stream().distinct().collect(Collectors.toList());

要么

new LinkedHashSet<>(new LinkedList<>(Arrays.asList(1,2,1,3)))

另請參閱https://www.geeksforgeeks.org/remove-duplicates-from-an-unsorted-linked-list

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM