#include <stdio.h>
#include <iostream>
using namespace std;
struct ListNode{
int data;
ListNode *next;
ListNode(int d){
data = d;
next = NULL;
}
};
ListNode* deleteElement(ListNode* head , int elem){
ListNode* dummy = new ListNode(-1);
ListNode* prev=dummy;
ListNode* cur=head;
while(cur){
while(cur && cur->data!= elem){
prev = cur;
cur = cur->next;
}
prev->next = cur->next; //
delete cur; //
cur = prev->next; //
}
return dummy->next;
}
void print(ListNode* head){
ListNode *p = head;
while(p){
cout<<p->data<<" ";
p=p->next;
}
}
void main(int argc, char** argv){
ListNode *p1= new ListNode(1);
ListNode *p2 = new ListNode(2);
ListNode *p3 = new ListNode(3);
ListNode *p4 = new ListNode(1);
ListNode *p5 = new ListNode(1);
ListNode *p6 = new ListNode(6);
ListNode *p7 = new ListNode(1);
p1->next = p2;
p2->next = p3;
p3->next = p4;
p4->next = p5;
p5->next = p6;
p6->next = p7;
int deleteElem = 1;
ListNode *res = deleteElement(p1, deleteElem);
print(res);
getchar();
return;
}
No comments:
Post a Comment