-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeletion and Reverse in Circular Linked List.cpp
56 lines (54 loc) · 1.33 KB
/
Deletion and Reverse in Circular Linked List.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
class Solution {
public:
// Function to reverse a circular linked list
Node* reverse(Node* head) {
if(head==NULL||head->next==head){
return head;
}
Node* prev=NULL;
Node* curr=head;
Node* next;
do{
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
}while(curr!=head);
head->next=prev;
head=prev;
return head;
}
// Function to delete a node from the circular linked list
Node* deleteNode(Node* head, int key) {
if(head==NULL){
return NULL;
}
if(head->data==key){
if(head->next==head){
delete head;
return NULL;
}
Node* last=head;
while(last->next!=head){
last=last->next;
}
last->next=head->next;
Node* temp=head;
head=head->next;
delete temp;
return head;
}
Node* prev;
Node* curr=head;
while(curr->next!=head){
if(curr->data==key){
prev->next=curr->next;
delete curr;
return head;
}
prev=curr;
curr=curr->next;
}
return head;
}
};