-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteAndReverseLL.cpp
More file actions
75 lines (63 loc) · 2.05 KB
/
deleteAndReverseLL.cpp
File metadata and controls
75 lines (63 loc) · 2.05 KB
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
Node* reverse(Node* head) {
// code here
if (head == NULL || head->next == head) {
return head; // Empty list or single node, no need to reverse
}
Node* prev = NULL;
Node* curr = head;
Node* next = NULL;
// First, store the original head
Node* originalHead = head;
while (curr->next != originalHead) { // Traverse until we are about to loop back
next = curr->next; // Store the next node
curr->next = prev; // Reverse the pointer
prev = curr; // Move prev forward
curr = next; // Move curr forward
}
// Handle the final node (when curr == originalHead)
next = curr->next;
curr->next = prev;
head->next = curr; // Make the last node point to the new head
head = curr;
return head;
}
// Function to delete a node from the circular linked list
Node* deleteNode(Node* head, int key) {
// code here
if (head == NULL) {
return NULL; // Empty list, nothing to delete
}
Node* curr = head;
Node* prev = NULL;
// Special case: If head node is to be deleted
if (head->data == key) {
// If there's only one node in the list
if (head->next == head) {
delete head;
return NULL;
}
// Find the last node in the list to update its next pointer
Node* last = head;
while (last->next != head) {
last = last->next;
}
// Update the last node's next pointer and delete the head
last->next = head->next;
Node* temp = head;
head = head->next;
delete temp;
return head;
}
// Traverse the list to find the node with the given key
while (curr->next != head) { // Stop if we return to the head
if (curr->next->data == key) {
Node* temp = curr->next;
curr->next = curr->next->next; // Unlink the node
delete temp;
return head;
}
curr = curr->next;
}
// If key wasn't found
return head;
}