从已排序的链表中删除重复项

huangapple go评论80阅读模式
英文:

Removing Duplicates from a Sorted Linked List

问题

以下是您要翻译的代码部分:

我已经在C++中编写了以下代码,用于从已排序的链表中删除重复项。

```cpp
#include <iostream>
using namespace std;

class Node
{
public:
    int data;
    Node *next;
    
    Node(int data)
    {
        this->data = data;
        this->next = NULL;
    }
};


void push(Node* &head, Node* &tail, int data)
{
    if(head==NULL)
    {
        Node* newNode = new Node(data);
        head = newNode;
        tail = newNode;
        return;
    }
    
    else
    {
        Node* newNode = new Node(data);
        tail->next = newNode;
        tail = newNode;
    }
}

void print(Node* &head)
{
    Node *temp = head;
    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp = temp->next;
    }
}

void removeDuplicates(Node* &head)
{
    if(head==NULL)
    {
        cout<<"Empty LL!";
        return;
    }
    
    if(head->next == NULL) 
    {
        cout << "Single Node in LL" << endl;
        return ;
    }
    
    Node* curr = head;
    
    while(curr!=NULL)
    {
        if(curr->next!=NULL && (curr->data == curr->next->data))
        {
            Node* temp = curr->next;
            curr->next = curr->next->next;
            temp->next = NULL;
            delete temp;
            
        }
        
        else
        {
             curr = curr->next;
        }
    }
}


int main()
{
    Node* head = NULL;
    Node* tail = NULL;
    
    push(head, tail, 25);
    push(head, tail, 50);
    push(head, tail, 50);
    push(head, tail, 67);
    print(head);
    cout<<endl;
    removeDuplicates(head);
    print(head);
    return 0;
}

虽然这段代码运行良好,但我的疑问是,在'if'块中删除重复节点后,为什么我们没有更新curr的值,比如curr = curr->next,以便将其发送到while循环中。否则while如何知道curr的更新值是什么?

PS:我仍然是C++的初学者 从已排序的链表中删除重复项


<details>
<summary>英文:</summary>
I&#39;ve written the following code in C++ which removes duplicates from a sorted Linked List.

#include <iostream>
using namespace std;

class Node
{
public:
int data;
Node *next;

Node(int data)
{
this-&gt;data = data;
this-&gt;next = NULL;
}

};

void push(Node* &head, Node* &tail, int data)
{
if(head==NULL)
{
Node* newNode = new Node(data);
head = newNode;
tail = newNode;
return;
}

else
{
Node* newNode = new Node(data);
tail -&gt; next = newNode;
tail = newNode;
}

}

void print(Node* &head)
{
Node *temp = head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}

void removeDuplicates(Node* &head)
{
if(head==NULL)
{
cout<<"Empty LL!";
return;
}

if(head -&gt; next == NULL) 
{
cout &lt;&lt; &quot;Single Node in LL&quot; &lt;&lt; endl;
return ;
}
Node* curr = head;
while(curr!=NULL)
{
if(curr-&gt;next!=NULL &amp;&amp; (curr-&gt;data == curr-&gt;next-&gt;data))
{
Node* temp = curr-&gt;next;
curr-&gt;next = curr-&gt;next-&gt;next;
temp-&gt;next = NULL;
delete temp;
}
else
{
curr = curr-&gt;next;
}
}

}

int main()
{
Node* head = NULL;
Node* tail = NULL;

push(head, tail, 25);
push(head, tail, 50);
push(head, tail, 50);
push(head, tail, 67);
print(head);
cout&lt;&lt;endl;
removeDuplicates(head);
print(head);
return 0;

}


While the code functions well, my doubt is that in the &#39;if&#39; block, after deleting the duplicate node, why aren&#39;t we updating the value of curr like curr = curr -&gt; next to send it to the while loop again. How else would while know what the updated value of curr is?
PS: Still a beginner in C++ :)
</details>
# 答案1
**得分**: 5
如果你从列表中移除下一个节点,可能仍然会有更多的重复项。如果你推进 `curr`,你只会删除每隔一个重复元素。
以下是当前代码的情况(`^`表示 `curr`,`---`表示下一次迭代,任何非数字节点内容仅用于标记不同的节点)
```plaintext
1(a) -&gt; 1(b) -&gt; 1(c) -&gt; 2(d)
^
---
1(a) -&gt; 1(c) -&gt; 2(d)
^
---
1(a) -&gt; 2(d)
^
---
1(a) -&gt; 2(d)
^

你建议的更改将导致以下结果

1(a) -&gt; 1(b) -&gt; 1(c) -&gt; 2(d)
^
---
1(a) -&gt; 1(c) -&gt; 2(d)
        ^
---
1(a) -&gt; 1(c) -&gt; 2(d)
                ^
英文:

If you remove the next node from the list, there may still be more duplicates left. If you'd advance curr, you'd only erase every other duplicate element.

The following happens with the current code (^ marks curr, --- the next iteration and any non-number node content is just there to mark the different nodes)

1(a) -&gt; 1(b) -&gt; 1(c) -&gt; 2(d)
^
---
1(a) -&gt; 1(c) -&gt; 2(d)
^
---
1(a) -&gt; 2(d)
^
---
1(a) -&gt; 2(d)
^

Your suggested change would result in the following

1(a) -&gt; 1(b) -&gt; 1(c) -&gt; 2(d)
^
---
1(a) -&gt; 1(c) -&gt; 2(d)
^
---
1(a) -&gt; 1(c) -&gt; 2(d)
^

答案2

得分: 3

因为你希望curr不受移除操作的影响,这样你可以将其与移除节点后的下一个节点进行比较。列表中可能有多个重复项,你希望移除除第一个以外的所有重复项。

英文:

> why aren't we updating the value of curr like curr = curr -&gt; next

Because you want curr unaffected by the removal so that you can compare it with the next node after the removed one too. There may be more than one duplicate in the list and you want all but the first one removed.

huangapple
  • 本文由 发表于 2023年6月11日 22:29:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定