为什么要保留“上一个”?删除链表中的重复项时?

发布于 2024-11-05 08:59:05 字数 513 浏览 0 评论 0原文

我想知道是否有人可以解释使用“LinkedListNode previous”变量的目的是什么。我了解尝试删除重复项的总体思路。您查看链表,如果某个值不在哈希表中,则将其插入。但如果是的话,它在做什么?我不太确定。

非常感谢您的帮助!如果有人能以清晰易懂的方式解释它,我将不胜感激。谢谢!

public static void deleteDups(LinkedListNode n) {
    Hashtable table = new Hashtable();
    LinkedListNode previous = null;
    while (n != null) {
        if (table.containsKey(n.data)) previous.next = n.next;
        else {
            table.put(n.data, true);
            previous = n;
        }
        n = n.next;
    }
}

i was wondering if anyone could explain what is the purpose of using the 'LinkedListNode previous' variable. i understand the general idea of trying to remove the duplicate. you look through the linked list, and if a value is not in the hashtable, insert it. but if it is, what is it doing? im not really sure.

thanks a bunch for helping! i would greatly appreciate it if someone could explain it in a clear and easy to understand way. thanks!

public static void deleteDups(LinkedListNode n) {
    Hashtable table = new Hashtable();
    LinkedListNode previous = null;
    while (n != null) {
        if (table.containsKey(n.data)) previous.next = n.next;
        else {
            table.put(n.data, true);
            previous = n;
        }
        n = n.next;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-11-12 08:59:05

如果没有“前一个”,链表 cons 链如何保持连接?

想象一下:如果要将 Current 项变为 Prev->Current->Next,则需要将其转换为 Prev->Next从列表中删除。如果 Prev 没有临时保存,则无法对其进行突变以更新链接。 (如果列表是双向链接列表,则不需要 Prev 因为它可以从 Current->previous 恢复)。

快乐编码。


回应评论中的问题:

如果不采取任何措施,则重复项目不会从列表中断开连接。 n = n.next 更改 n 的值(但不会更改存储在 n 或其他位置的元素/节点的数据,因此 < code>Prev.next 从未从 Current 更改)。

需要做的是Prevnext,被删除元素之前的元素/节点,必须更新为引用已删除元素之后的元素 (Next)。

previous.next = n.next; // and this does it

(这也可以用函数式非突变风格编写,其中原始列表不会被修改,并且会构建一个新列表 - 在这种情况下,就不需要 Prev)。

Without the "previous", how would the linked-list cons chain remain connected?

Imagine: Prev->Current->Next which needs to be turned into Prev->Next if the Current item is to be removed from the list. If Prev wasn't saved temporarily then it couldn't be mutated to update the linking. (If the list was a doubly-linked list then Prev wouldn't be needed because it could be restored from Current->previous).

Happy coding.


In response to question in comment:

If nothing is done then the duplicate item is not disconnect from the list. n = n.next changes the value of n (but does not change the data of the element/node stored in n or elsewhere so Prev.next is never changed from Current).

What needs to be done is the the next of Prev, the element/node before the deleted element, must be updated to refer to the element (Next) after the deleted element.

previous.next = n.next; // and this does it

(This could also be written in a functional non-mutative style where the original list is not modified and a new list is built -- in that case there would be no need for Prev).

天荒地未老 2024-11-12 08:59:05
public static void deleteDup( LinkedList* h){

    if( !h || !h->next )
        return;
    Hashtable ht = new Hashtable();
    LinkedListNode* previous = h;
    LinkedListNode* curr = h->next;
    while( curr ){
        if( ht.containsKey() ){
            previous->next = curr->next;
            free(curr);
            curr = previous->next;

        }
        else{
            ht.put(curr->data, true );
             previous = curr;
             curr = curr->next;
        }
    }
}   
public static void deleteDup( LinkedList* h){

    if( !h || !h->next )
        return;
    Hashtable ht = new Hashtable();
    LinkedListNode* previous = h;
    LinkedListNode* curr = h->next;
    while( curr ){
        if( ht.containsKey() ){
            previous->next = curr->next;
            free(curr);
            curr = previous->next;

        }
        else{
            ht.put(curr->data, true );
             previous = curr;
             curr = curr->next;
        }
    }
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文