14
14
// 10. delete last kth
15
15
// 11. find middle node
16
16
17
+ // 12. remove =k elements
18
+ // 13. rotate right
19
+ // 14. get intersection
20
+ // 15. check palinkdrome 12321
21
+
17
22
typedef struct SinglyLinkedNode {
18
23
int data ;
19
24
struct SinglyLinkedNode * next ;
@@ -63,6 +68,29 @@ void print_linked_list(SinglyLinkedNode *head) {
63
68
}
64
69
}
65
70
71
+ void test_create_print_free () {
72
+ SinglyLinkedNode * head = NULL ;
73
+ append_node (& head , 1 );
74
+ append_node (& head , 2 );
75
+ append_node (& head , 3 );
76
+ append_node (& head , 4 );
77
+
78
+ print_linked_list (head );
79
+
80
+ free_linked_list (head );
81
+ }
82
+
83
+ void test_insert () {
84
+ SinglyLinkedNode * head = NULL ;
85
+ insert_node (& head , 3 );
86
+ insert_node (& head , 2 );
87
+ insert_node (& head , 1 );
88
+
89
+ print_linked_list (head );
90
+
91
+ free_linked_list (head );
92
+ }
93
+
66
94
void reverse (SinglyLinkedNode * * head_ref ) {
67
95
SinglyLinkedNode * current = * head_ref ;
68
96
SinglyLinkedNode * prev_node = NULL ;
@@ -77,6 +105,20 @@ void reverse(SinglyLinkedNode **head_ref) {
77
105
}
78
106
}
79
107
108
+ void test_reverse () {
109
+ SinglyLinkedNode * head = NULL ;
110
+ append_node (& head , 1 );
111
+ append_node (& head , 2 );
112
+ append_node (& head , 3 );
113
+ print_linked_list (head );
114
+
115
+ printf ("---\n" );
116
+ reverse (& head );
117
+ print_linked_list (head );
118
+
119
+ free_linked_list (head );
120
+ }
121
+
80
122
int check_circle (SinglyLinkedNode * head ) {
81
123
SinglyLinkedNode * slow , * fast ;
82
124
slow = fast = head ;
@@ -90,6 +132,21 @@ int check_circle(SinglyLinkedNode *head) {
90
132
return 0 ;
91
133
}
92
134
135
+ void test_circle () {
136
+ SinglyLinkedNode * head = NULL ;
137
+ append_node (& head , 1 );
138
+ append_node (& head , 2 );
139
+ append_node (& head , 3 );
140
+ append_node (& head , 4 );
141
+
142
+ head -> next -> next -> next -> next = head ;
143
+
144
+ int has_circle = check_circle (head );
145
+ printf ("has circle = %d\n" , has_circle );
146
+
147
+ free_linked_list (head );
148
+ }
149
+
93
150
SinglyLinkedNode * find_middle_node (SinglyLinkedNode * head ) {
94
151
SinglyLinkedNode * slow , * fast ;
95
152
slow = fast = head ;
@@ -100,6 +157,20 @@ SinglyLinkedNode *find_middle_node(SinglyLinkedNode *head) {
100
157
return slow ;
101
158
}
102
159
160
+ void test_middle_node () {
161
+ SinglyLinkedNode * head = NULL ;
162
+ append_node (& head , 1 );
163
+ append_node (& head , 2 );
164
+ append_node (& head , 3 );
165
+ append_node (& head , 4 );
166
+ append_node (& head , 5 );
167
+
168
+ SinglyLinkedNode * middle_node = find_middle_node (head );
169
+ printf ("middle node value = %d\n" , middle_node -> data );
170
+
171
+ free_linked_list (head );
172
+ }
173
+
103
174
void delete_last_kth (SinglyLinkedNode * * head_ref , int k ) {
104
175
// 1. fast move k step
105
176
SinglyLinkedNode * fast = * head_ref ;
@@ -127,6 +198,22 @@ void delete_last_kth(SinglyLinkedNode **head_ref, int k) {
127
198
free (slow );
128
199
}
129
200
201
+ void test_delete_last_kth () {
202
+ SinglyLinkedNode * head = NULL ;
203
+ append_node (& head , 1 );
204
+ append_node (& head , 2 );
205
+ append_node (& head , 3 );
206
+ append_node (& head , 4 );
207
+ append_node (& head , 5 );
208
+ print_linked_list (head );
209
+
210
+ delete_last_kth (& head , 2 );
211
+ print_linked_list (head );
212
+
213
+ free_linked_list (head );
214
+ }
215
+
216
+
130
217
// 1 3 5 | 2 4 6 -> 3 5 | 2 4 6
131
218
void move_node (SinglyLinkedNode * * dest_ref , SinglyLinkedNode * * src_head_ref ) {
132
219
SinglyLinkedNode * new_node = * src_head_ref ;
@@ -159,87 +246,6 @@ SinglyLinkedNode *merge_sorted_linked_list(SinglyLinkedNode *a , SinglyLinkedNod
159
246
return result .next ;
160
247
}
161
248
162
- void test_create_print_free () {
163
- SinglyLinkedNode * head = NULL ;
164
- append_node (& head , 1 );
165
- append_node (& head , 2 );
166
- append_node (& head , 3 );
167
- append_node (& head , 4 );
168
-
169
- print_linked_list (head );
170
-
171
- free_linked_list (head );
172
- }
173
-
174
- void test_insert () {
175
- SinglyLinkedNode * head = NULL ;
176
- insert_node (& head , 3 );
177
- insert_node (& head , 2 );
178
- insert_node (& head , 1 );
179
-
180
- print_linked_list (head );
181
-
182
- free_linked_list (head );
183
- }
184
-
185
- void test_reverse () {
186
- SinglyLinkedNode * head = NULL ;
187
- append_node (& head , 1 );
188
- append_node (& head , 2 );
189
- append_node (& head , 3 );
190
- print_linked_list (head );
191
-
192
- printf ("---\n" );
193
- reverse (& head );
194
- print_linked_list (head );
195
-
196
- free_linked_list (head );
197
- }
198
-
199
- void test_circle () {
200
- SinglyLinkedNode * head = NULL ;
201
- append_node (& head , 1 );
202
- append_node (& head , 2 );
203
- append_node (& head , 3 );
204
- append_node (& head , 4 );
205
-
206
- head -> next -> next -> next -> next = head ;
207
-
208
- int has_circle = check_circle (head );
209
- printf ("has circle = %d\n" , has_circle );
210
-
211
- free_linked_list (head );
212
- }
213
-
214
- void test_middle_node () {
215
- SinglyLinkedNode * head = NULL ;
216
- append_node (& head , 1 );
217
- append_node (& head , 2 );
218
- append_node (& head , 3 );
219
- append_node (& head , 4 );
220
- append_node (& head , 5 );
221
-
222
- SinglyLinkedNode * middle_node = find_middle_node (head );
223
- printf ("middle node value = %d\n" , middle_node -> data );
224
-
225
- free_linked_list (head );
226
- }
227
-
228
- void test_delete_last_kth () {
229
- SinglyLinkedNode * head = NULL ;
230
- append_node (& head , 1 );
231
- append_node (& head , 2 );
232
- append_node (& head , 3 );
233
- append_node (& head , 4 );
234
- append_node (& head , 5 );
235
- print_linked_list (head );
236
-
237
- delete_last_kth (& head , 2 );
238
- print_linked_list (head );
239
-
240
- free_linked_list (head );
241
- }
242
-
243
249
void test_merge_sorted_linked_list () {
244
250
SinglyLinkedNode * a = NULL ;
245
251
append_node (& a , 1 );
0 commit comments