-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindromeLinkedList.c
109 lines (89 loc) · 2.5 KB
/
palindromeLinkedList.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Definition for singly-linked list.
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to find the middle of the linked list
struct Node* findMiddle(struct Node* head) {
struct Node* slow = head;
struct Node* fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
// Function to reverse a linked list
struct Node* reverseList(struct Node* head) {
struct Node* prev = NULL;
struct Node* current = head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
// Function to check if the linked list is a palindrome
bool isPalindrome(struct Node* head) {
if (head == NULL || head->next == NULL) {
return true;
}
// Find the middle of the linked list
struct Node* middle = findMiddle(head);
// Reverse the second half of the linked list
struct Node* secondHalf = reverseList(middle);
// Compare the first and second halves
struct Node* firstHalf = head;
struct Node* temp = secondHalf;
bool palindrome = true;
while (temp != NULL) {
if (firstHalf->data != temp->data) {
palindrome = false;
break;
}
firstHalf = firstHalf->next;
temp = temp->next;
}
// Restore the original list (optional)
reverseList(secondHalf);
return palindrome;
}
// Function to print the linked list
void printList(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
// Create a linked list: 1 -> 2 -> 3 -> 2-> 1
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(2);
head->next->next->next->next = createNode(1);
// Print the original list
printf("Original List: ");
printList(head);
// Check if the list is a palindrome
if (isPalindrome(head)) {
printf("The list is a palindrome.\n");
} else {
printf("The list is not a palindrome.\n");
}
return 0;
}