-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeletion_and_Reverse_in_CLL.cpp
69 lines (63 loc) · 1.59 KB
/
Deletion_and_Reverse_in_CLL.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
class Solution
{
public:
// Function to reverse a circular linked list
Node *reverse(Node *head)
{
// code here
Node *s, *curr, *temp, *prev;
curr = head;
temp = prev = NULL;
do
{
temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
} 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)
{
Node *p, *s;
p = head;
if (p->data == key)
{
s = head;
while (s->next != head)
{
s = s->next;
}
s->next = p->next;
p = s->next;
head = p;
return head;
}
else
{
p = head;
bool found = false;
while (p->next != head)
{
if (p->next->data == key)
{
found = true;
break;
}
p = p->next;
}
if (found)
{
p->next = (p->next)->next;
return head;
}
else
{
return head;
}
}
}
};