-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path234_linkedListPalindrome.c
58 lines (51 loc) · 1.01 KB
/
234_linkedListPalindrome.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
struct Stack{
int top;
int cap;
int *array;
};
struct Stack *createStack(int cap){
struct Stack *S = malloc(sizeof(struct Stack));
S->cap = cap;
S->top = -1;
S->array = malloc(S->cap*sizeof(int));
//dynamically allocate here too, not simple array declaration
return S;
}
void push(struct Stack* S, int data){
S->array[++S->top] = data;
}
int pop(struct Stack* S){
return(S->array[S->top--]);
}
bool isPalindrome(struct ListNode* head){
struct ListNode* cur = head;
int count=0;
while(cur!=NULL){
count++;
cur=cur->next;
}
cur=head;
struct Stack* S = createStack(count);
while(cur!=NULL){
push(S, cur->val);
cur=cur->next;
}
cur=head;
int flag;
while(cur!=NULL){
if(cur->val == pop(S)){
flag=1;
}
else{
flag=0;
break;
}
cur=cur->next;
}
if(flag==1){
return 1;
}
else{
return 0;
}
}