-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularLLbasic.cpp
More file actions
150 lines (140 loc) · 3.27 KB
/
Copy pathcircularLLbasic.cpp
File metadata and controls
150 lines (140 loc) · 3.27 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
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
139
140
141
142
143
144
145
146
147
148
149
150
#include <bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* next;
Node(int x){
data = x;
next = NULL;
}
};
void traverse_circularLL(Node* head){ //O(n) time
if(head == NULL){
return;
}
Node *p = head;
do{
cout << p -> data << " ";
p = p -> next;
}while(p != head);
}
Node* insertAtBeginning_naive(Node* head, int x){ //O(n) time
Node* temp = new Node(x);
if(head == NULL){
temp -> next = temp;
}
else{
Node* curr = head;
while(curr -> next != head){
curr = curr -> next;
}
curr -> next = temp;
temp -> next = head;
}
return temp;
}
Node* insertAtBeginning_efficient(Node* head, int x){ //O(1) time
Node* temp = new Node(x);
if(head == NULL){
temp -> next = temp;
return temp;
}
else{
temp -> next = head -> next;
head -> next = temp;
int t = temp -> data;
temp -> data = head -> data;
head -> data = t;
return head;
}
}
Node* insertAtend_naive(Node*head, int x){ //theta(n) time
Node* temp = new Node(x);
if(head == NULL){
temp -> next = temp;
return temp;
}
else{
Node* curr = head;
while(curr -> next != head){
curr = curr -> next;
}
curr -> next = temp;
temp -> next = head;
return head;
}
}
Node* insertatend_efficient(Node* head, int x){ //O(1) time
Node* temp = new Node(x);
if(head == NULL){
temp -> next = temp;
return temp;
}
else{
temp -> next = head -> next;
head -> next = temp;
int t = temp -> data;
temp -> data = head -> data;
head -> data = t;
return temp;
}
}
Node* delete_headNaive(Node* head){ //theta(n) time
if(head == NULL){
return NULL;
}
if(head -> next == head){
delete head;
return NULL;
}
Node* curr = head;
while(curr -> next != head){
curr = curr -> next;
}
curr -> next = head -> next;
delete head;
return curr -> next;
}
Node* delete_headEfficient(Node* head){
if(head == NULL){
return NULL;
}
if(head -> next == head){
delete head;
return NULL;
}
Node* temp = head -> next;
head -> data = temp -> data;
head -> next = temp -> next;
delete temp;
return head;
}
Node* delete_kthNode(Node* head, int k){ //O(1) time
if(head == NULL){
return head;
}
if(k == 1){
return delete_headEfficient(head);
}
Node* curr = head;
for(int i = 0; i < k - 2; i++){ //k - 2 because we want our curr to stop at (k - 1)th node
curr = curr -> next;
}
curr -> next = curr -> next -> next;
delete curr -> next;
return head;
}
int main(int argc, char** argv){
// Node* head = new Node(10);
// head = new Node(20);
Node* head = NULL;
head = insertAtBeginning_efficient(head, 10);
head = insertAtBeginning_efficient(head, 20);
head = insertAtend_naive(head, 30);
head = insertatend_efficient(head, 15);
// head = delete_kthNode(head, 2);
// head = delete_headNaive(head);
// head = delete_headEfficient(head);
traverse_circularLL(head);
return 0;
}