-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathFindLinkedListPalindrome.c
145 lines (117 loc) · 3.17 KB
/
FindLinkedListPalindrome.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
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
/* C program to find whether a linked list is palindrome or not.
We need to detect whether the front half of the list is the reverse of the second half.
By reversing the front half of the list and for that stack can be used.
We know the size of the linked list, we can iterate onto the first half of the elements in a for loop,
pushing each element onto a stack. Now, we iterate the rest of the linked list. At each step, we compare
the node to the top of the stack. If we complete the iteration without finding the difference,
then the linked list is a palindrome.
*/
#include <bits/stdc++.h>
#include <stdio.h>
// structure of a node in a linked list
struct Node {
int value;
struct Node *next;
};
struct Node *head = NULL;
// Dynamically generate the node
struct Node *gen_node(int val) {
struct Node *newnode;
newnode = (struct Node *)malloc(sizeof(struct Node));
if (newnode == NULL) {
printf("\nMemory was not allocated");
return 0;
} else {
newnode->value = val;
newnode->next = NULL;
return newnode;
}
}
// Insert node at the beginning of the linked list
void insert_node_first() {
int val;
struct Node *newNode, *temp;
printf("\nEnter the value for the node:");
scanf("%d", &val);
newNode = gen_node(val);
if (head == NULL) {
head = newNode;
head->next = NULL;
} else {
temp = head;
head = newNode;
head->next = temp;
}
}
// Find palindrome in the linked list
int find_Palindrome(int size) {
int arr[size / 2], i = 0, j = 0;
struct Node *fast = head;
struct Node *slow = head;
/* Push elements from first half of the linked list onto array.
When fast pointer (which is moving at 2x speed)reaches the end of the linked
list, then we know we are at the middle
*/
while (fast != NULL && fast->next != NULL) {
arr[i] = slow->value;
slow = slow->next;
fast = fast->next->next;
i++;
}
// Has odd number of elements,so skip the middle elements
if (fast != NULL) {
slow = slow->next;
}
j = i - 1;
while (slow != NULL) {
int val = arr[j];
// If the value are different ,then it's not a palindrome
if (val != slow->value) {
return 0;
}
slow = slow->next;
j--;
}
return 1;
}
// Display the entered list
void display() {
struct Node *temp;
temp = head;
while (temp != NULL) {
printf("%d ", temp->value);
temp = temp->next;
}
}
int main() {
int size, i, val, status;
printf("Enter the size of the Linked List:");
scanf("%d", &size);
for (i = 0; i < size; i++) {
insert_node_first();
}
printf("\nLinked List after Insertion:");
display();
status = find_Palindrome(size);
if (status) {
printf("\nThe above Linked List is Palindrome");
} else {
printf("\nThe above Linked List is Not Palindrome");
}
return 0;
}
/*
Sample input/output:
Enter the size of the Linked List:7
Enter the value for the node:1
Enter the value for the node:2
Enter the value for the node:3
Enter the value for the node:4
Enter the value for the node:3
Enter the value for the node:2
Enter the value for the node:1
Linked List after Insertion:1 2 3 4 3 2 1
The above Linked List is Palindrome
Time complexity: O(n)
Space complexity: O(n)
*/