-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOccurenceOfKeyinDLL.cpp
51 lines (47 loc) · 1.34 KB
/
OccurenceOfKeyinDLL.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
class Solution {
public:
void deleteAllOccurOfX(struct Node** head_ref, int x) {
// Write your code here
/*
Node*temp=*head_ref;
while(temp!=NULL){
// occurence is at 1st node
if(temp->data==x){
if(temp->prev==NULL){
*head_ref=temp->next;
temp->prev=NULL;
}
// occurence is at last node
else if(temp->next==NULL){
temp->prev->next=NULL;
}
else{
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
}
}
temp=temp->next;
}
*/
Node*temp=*head_ref;
while(temp!=NULL){
// occurence is at 1st node
if(temp->data==x){
if(temp->prev==NULL){
*head_ref=temp->next;
temp->prev=NULL;
}
// occurence is at last node
else if(temp->next==NULL){
temp->prev->next=NULL;
}
// occurence is in middle
else{
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
}
}
temp=temp->next;
}
}
};