-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeletion_and_Reverse_in_Circular_Linked_List.cpp
54 lines (53 loc) · 1.25 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
class Solution {
public:
// Function to reverse a circular linked list
Node* reverse(Node* head) {
Node* next_=head->next;
Node* temp=NULL;
Node* last = head;
while(next_!=last)
{
temp=next_->next;
next_->next=head;
head=next_;
next_=temp;
}
last->next = head;
return head;
}
// Function to delete a node from the circular linked list
Node* deleteNode(Node* head, int key) {
if(!head->next)
return NULL;
Node* prev = head;
Node* curr = head->next;
Node* ansNode = NULL;
if(head->data==key)
{
ansNode = head->next;
while(curr!=head)
{
prev=curr;
curr=curr->next;
}
prev->next=curr->next;
return ansNode;
}
else{
ansNode = head;
}
Node* flag = curr;
do
{
if(curr->data==key)
{
prev->next = curr->next;
return ansNode;
}
prev = curr;
curr = curr->next;
}
while(curr!=flag);
return ansNode;
}
};