forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome of a LinkedList.c
More file actions
79 lines (66 loc) · 1.91 KB
/
Palindrome of a LinkedList.c
File metadata and controls
79 lines (66 loc) · 1.91 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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Definition for singly-linked list node
struct ListNode {
int val;
struct ListNode *next;
};
// Helper function to create new node
struct ListNode* newNode(int val) {
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->val = val;
node->next = NULL;
return node;
}
// Reverse a linked list and return new head
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode *prev = NULL, *next = NULL, *curr = head;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Check if linked list is palindrome
bool isPalindrome(struct ListNode* head) {
if (!head || !head->next) return true;
// Step 1: Find middle using slow-fast pointers
struct ListNode *slow = head, *fast = head;
while (fast->next && fast->next->next) {
slow = slow->next;
fast = fast->next->next;
}
// Step 2: Reverse second half
struct ListNode* secondHalf = reverseList(slow->next);
// Step 3: Compare both halves
struct ListNode* firstHalf = head;
struct ListNode* temp = secondHalf;
bool palindrome = true;
while (temp) {
if (firstHalf->val != temp->val) {
palindrome = false;
break;
}
firstHalf = firstHalf->next;
temp = temp->next;
}
// Step 4: (Optional) Restore original list
slow->next = reverseList(secondHalf);
return palindrome;
}
// Example usage
int main() {
// Create linked list: 1 -> 2 -> 2 -> 1
struct ListNode* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(2);
head->next->next->next = newNode(1);
if (isPalindrome(head))
printf("The linked list is a palindrome.\n");
else
printf("The linked list is not a palindrome.\n");
return 0;
}