forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(GFG,Leetcode)RemoveDuplicatesFromSinglyLL.cpp
64 lines (57 loc) · 1.17 KB
/
(GFG,Leetcode)RemoveDuplicatesFromSinglyLL.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node *next;
Node(int x){
data=x;
next=NULL;
}
};
Node *insertend(Node *head,int x){
Node *temp = new Node(x);
if(head == NULL){
return temp;
}
Node *curr = head;
while(curr->next != NULL){
curr = curr->next;
}
curr->next = temp;
return (head);
}
Node *remdul(Node *head){
Node *curr = head;
while(curr != NULL && curr->next != NULL){
if(curr->data == curr->next->data){
Node *temp = curr->next;
curr->next = curr->next->next;
delete temp;
}
else{
curr = curr->next;
}
}
return head;
}
void printL(Node *head){
if(head == NULL){
return;
}
cout<<head->data<<" ";
printL(head->next);
}
int main(){
Node *head = NULL;
head = insertend(head,10);
head = insertend(head,20);
head = insertend(head,30);
head = insertend(head,40);
head = insertend(head,40);
head = insertend(head,50);
head = insertend(head,50);
head = insertend(head,50);
head = remdul(head);
printL(head);
return 0;
}