英文:
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'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->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;
}
While the code functions well, my doubt is that in the 'if' block, after deleting the duplicate node, why aren't we updating the value of curr like curr = curr -> 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) -> 1(b) -> 1(c) -> 2(d)
^
---
1(a) -> 1(c) -> 2(d)
^
---
1(a) -> 2(d)
^
---
1(a) -> 2(d)
^
你建议的更改将导致以下结果
1(a) -> 1(b) -> 1(c) -> 2(d)
^
---
1(a) -> 1(c) -> 2(d)
^
---
1(a) -> 1(c) -> 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) -> 1(b) -> 1(c) -> 2(d)
^
---
1(a) -> 1(c) -> 2(d)
^
---
1(a) -> 2(d)
^
---
1(a) -> 2(d)
^
Your suggested change would result in the following
1(a) -> 1(b) -> 1(c) -> 2(d)
^
---
1(a) -> 1(c) -> 2(d)
^
---
1(a) -> 1(c) -> 2(d)
^
答案2
得分: 3
因为你希望curr
不受移除操作的影响,这样你可以将其与移除节点后的下一个节点进行比较。列表中可能有多个重复项,你希望移除除第一个以外的所有重复项。
英文:
> why aren't we updating the value of curr
like curr = curr -> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论