-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPalindromeLinkList.cpp
More file actions
37 lines (34 loc) · 874 Bytes
/
PalindromeLinkList.cpp
File metadata and controls
37 lines (34 loc) · 874 Bytes
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
/*
Palindrome List
Given a singly linked list, determine if its a palindrome. Return 1 or 0 denoting if its a palindrome or not, respectively. Notes:
Expected solution is linear in time and constant in space.
For example,
List 1-->2-->1 is a palindrome.
List 1-->2-->3 is not a palindrome.
*/
// Push everything to stack and compare all elements or reverse the list and compare
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
int Solution::lPalin(ListNode* A) {
stack<int> s;
ListNode *head=A;
while(head!=NULL){
s.push(head->val);
head=head->next;
}
//ListNode *temp=A;
while(A!=NULL){
if(A->val != s.top()){
return 0;
}
A=A->next;
s.pop();
}
return 1;
}