-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeletion_and_reverse_in_circular_linked_list.cpp
138 lines (118 loc) · 3.34 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Deletion and Reverse in Circular Linked List
/*
Given a Circular Linked List.
The task is to delete the given node, key in the circular linked list, and reverse the circular linked list.
Note: You don't have to print anything, just return head of the modified list in each function.
Example1:
Input: Linked List: 2->5->7->8->10, key = 8
Output: 10->7->5->2
Explanation:
After deleting 8 from the given circular linked list, it has elements as 2, 5, 7, 10.
Now, reversing this list will result in 10, 7, 5, 2.
Example2:
Input: Linked List: 1->7->8->10, key = 8
Output: 10->7->1
Explanation:
After deleting 8 from the given circular linked list, it has elements as 1, 7,10.
Now, reversing this list will result in 10, 7, 1.
Expected Time Complexity: O(n)
Expected Auxillary Space: O(1)
Constraints:
2 <= number of nodes, key <= 10^5
1 <= node -> data <= 10^5
*/
#include <bits/stdc++.h>
using namespace std;
// Structure for the linked list node
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
void printList(struct Node *head) {
if (head != NULL) {
struct Node *temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
} else {
cout << "empty" << endl;
}
cout << endl;
}
class Solution {
public:
// Function to reverse a circular linked list
Node* reverse(Node* head) {
Node *prev = head, *curr = head, *next;
while(prev->next != head)
prev = prev->next;
while(next != head) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to delete a node from the circular linked list
Node* deleteNode(Node* head, int key) {
Node *prev = head, *curr = head->next;
if(head->data == key) {
while(prev->next != head)
prev = prev->next;
prev->next = head->next;
free(head);
return prev->next;
}
while(curr != head) {
if(curr->data == key) {
prev->next = curr->next;
free(curr);
return head;
}
prev = curr;
curr = curr->next;
}
return head;
}
};
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
vector<int> arr;
string input;
getline(cin, input);
stringstream ss(input);
int number;
// Reading input into a vector
while (ss >> number) {
arr.push_back(number);
}
int key;
cin >> key;
cin.ignore();
// Creating the circular linked list
struct Node *head = new Node(arr[0]);
struct Node *tail = head;
for (int i = 1; i < arr.size(); ++i) {
tail->next = new Node(arr[i]);
tail = tail->next;
}
tail->next = head; // Make the list circular
Solution ob;
// Delete the node with the given key
head = ob.deleteNode(head, key);
// Reverse the circular linked list
head = ob.reverse(head);
// Print the modified list
printList(head);
}
return 0;
}