-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathnth_element_from_last.c
90 lines (75 loc) · 1.9 KB
/
nth_element_from_last.c
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
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insert(struct Node** head_ref, int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
if (new_node == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}
new_node->data = data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
} else {
struct Node* last = *head_ref;
while (last->next != NULL) {
last = last->next;
}
last->next = new_node;
}
}
struct Node* findNthFromEnd(struct Node* head, int n) {
if (head == NULL || n <= 0) {
fprintf(stderr, "Invalid input\n");
exit(1);
}
struct Node* fast = head;
struct Node* slow = head;
for (int i = 0; i < n; i++) {
if (fast == NULL) {
fprintf(stderr, "N is larger than the list size\n");
exit(1);
}
fast = fast->next;
}
while (fast != NULL) {
fast = fast->next;
slow = slow->next;
}
return slow;
}
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
void freeList(struct Node* head) {
while (head != NULL) {
struct Node* temp = head;
head = head->next;
free(temp);
}
}
int main() {
struct Node* head = NULL;
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
insert(&head, 4);
insert(&head, 5);
printf("Linked List: ");
printList(head);
int n = 3;
struct Node* result = findNthFromEnd(head, n);
if (result != NULL) {
printf("The %dth node from the end is: %d\n", n, result->data);
}
freeList(head); // Free memory
return 0;
}