Quesn链接
我已经写了代码,但它没有给出正确的输出,但从逻辑上讲它是正确的,我不知道为什么它给出了错误的输出,甚至我多次怀疑它,但我认为这个问题击中了我的盲点。。所以Plz帮我和我这个代码运行Plz-Plz-
#include<iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
void print(Node *root){
while(root!=NULL){
cout<<root->data<<" ";
root=root->next;
}
cout<<endl;
}
class Solution
{ private:
void solve(Node* ¤t,Node* &nxt){
if(nxt==NULL){
return;
}
Node *temp=nxt;
Node *previous=current;;
while(nxt!=NULL){
if(current->data==nxt->data){
if(nxt->next=NULL){
delete nxt;
previous->next=NULL;
return;
}
previous->next=nxt->next;
delete nxt;
nxt=previous->next;
}else{
nxt=nxt->next;
previous=previous->next;
}
}
nxt=temp;
solve(current->next,nxt->next);
}
public:
//Function to remove duplicates from unsorted linked list.
Node * removeDuplicates( Node *head)
{ if(head==NULL){
return NULL;
}`enter code here`
if(head->next==NULL){
return head;
}
Node *temp=head;
Node *next=temp->next;
solve(head,next);
return temp;
}
};
int main(){
int k;
cin>>k;
struct Node *head=NULL;
struct Node *temp=head;
for(int i=0;i<k;i++){
int data;
cin>>data;
if(head==NULL){
head=temp=new Node(data);
}else{
temp->next=new Node(data);
temp=temp->next;
}
}
Solution ob;
Node *result=ob.removeDuplicates(head);
print(result);
return 0;
}
看到亮点部分,我认为问题从部分开始
快速扫描显示此错误
if (nxt->next = NULL)
我打赌你是指
if (nxt->next == NULL)
我的编译器好心地警告我
1>C: \work\ConsoleApplication1\ConsoleApplication1.cpp(40):警告C4706:条件表达式中的赋值