-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep63_LL_3_duplicate_1.cpp
More file actions
91 lines (81 loc) · 2.11 KB
/
practicep63_LL_3_duplicate_1.cpp
File metadata and controls
91 lines (81 loc) · 2.11 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
using namespace std;
//Remove Duplicates from Sorted List using linked list
class Node{
public:
int data;
Node* next;
Node(int data){
this->data=data;
this->next=NULL;
};
};
class Solution{
public:
Node* removeduplicates(Node* head){
Node* nodupli=new Node(0);//just a dummy node(new ll for non duplicates)
Node*temp=nodupli;
// Traverse the original list
while(head!=NULL){
// Check if the current node is unique (last node or not equal to the next node)
if(head->next==NULL|| head->data!=head->next->data){
temp->next=head;
temp=temp->next;
}
head=head->next; //next node of the original array.
};
return nodupli->next; //head return(skipping the 1st unwanted node)
};
void insert(Node*&head,Node*&tail,int data){
Node* newNode=new Node(data);
if(head==NULL || tail==NULL){
head=newNode;
tail=newNode;
return;
};
tail->next=newNode;
tail=newNode;
};
void print(Node* head){
if(head==NULL){
cout<<"Empty LinkedList"<<endl;
return;
};
Node*temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
};
}
};
int main(){
Node* head=NULL;
Node*tail=NULL;
Solution S;
cout<<"1:insert in sorted manner\n2:print with duplicates\n3:print without duplicates\n";
while(true){
int ch;
cin>>ch;
switch (ch){
case 1:
int d;
cout<<"enter value: ";
cin>>d;
S.insert(head,tail,d);
break;
case 2:
cout<<"List with duplicates: ";
S.print(head);
cout<<endl;
break;
case 3:
cout<<"List with no duplicates: ";
S.print(S.removeduplicates(head));
cout<<endl;
break;
case 4:
return false;
break;
}
}
}