-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLL-RemoveDuplicates.cpp
More file actions
42 lines (38 loc) · 883 Bytes
/
LL-RemoveDuplicates.cpp
File metadata and controls
42 lines (38 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* prev;
Node* next;
Node(int data){
this -> data= data;
this -> next= NULL;
this -> prev= NULL;
}
~Node(){
int value= this->data;
if(this->next!=NULL){
delete next;
this->next=NULL;
}
cout<<"memory is free for data "<<value<<endl;
}
};
Node* removeDuplicates(Node* head){
if(head==NULL)
return head;
Node* curr= head;
while(curr->next!=NULL){
if(curr->data==curr->next->data){
Node* next_next= curr->next->next;
Node* nodeToDelete= curr->next;
delete(nodeToDelete);
curr->next= next_next;
}
else{
curr=curr->next;
}
}
return head;
}