-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeletion and Reverse in Circular Linked List.cpp
59 lines (52 loc) · 1.65 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
57
58
59
class Solution {
public:
// Function to reverse a circular linked list
Node* reverse(Node* head) {
// If the list is empty or contains only one node, no need to reverse
if (head == NULL || head->next == head) {
return head;
}
Node* prev = NULL;
Node* current = head;
Node* next = NULL;
// This loop will reverse the links of the circular linked list
do {
next = current->next; // Store next node
current->next = prev; // Reverse the link
prev = current; // Move prev to current
current = next; // Move current to next
} while (current != head); // Stop when we complete the full circle
// At this point, current is at the head node
// head should now point to the last node
head->next = prev;
head = prev; // Update the head to be the new first node
return head;
}
// Function to delete a node from the circular linked list
Node* deleteNode(Node* head, int key) {
// code here
Node* temp = head;
Node* prev;
// Search for the element
while(temp->next != head && temp->data != key){
prev = temp;
temp = temp->next;
}
// Element found
if(temp->data == key){
if(temp == head){
Node* itr = head;
while(itr->next != head){
itr = itr->next;
}
itr->next = head->next;
head = itr->next;
}
else{
prev->next = temp->next;
}
delete temp;
}
return head;
}
};