-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindromeLL.c
More file actions
84 lines (62 loc) · 1.43 KB
/
palindromeLL.c
File metadata and controls
84 lines (62 loc) · 1.43 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
80
81
82
83
84
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int val;
struct Node* next;
} Node;
Node* createNode(int val){
Node* node = (Node*)malloc(sizeof(Node));
node->val = val;
node->next = NULL;
return node;
}
Node* createLL(int val, Node* head){
if (!head) return createNode(val);
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = createNode(val);
return head;
}
Node* reverseLL(Node* head){
Node* prev = NULL;
Node* curr = head;
Node* next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
int palindromeCheck(Node* head){
if (head == NULL || head->next == NULL) return 1;
Node* slow = head;
Node* fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
Node* second = reverseLL(slow);
Node* first = head;
Node* temp = second;
while (temp != NULL) {
if (temp->val != first->val) return 0;
temp = temp->next;
first = first->next;
}
return 1;
}
int main(){
Node* head = NULL;
head = createLL(1, head);
head = createLL(2, head);
head = createLL(3, head);
if (palindromeCheck(head)) {
printf("hell yeah\n");
} else {
printf("fuck y'all\n");
}
}