Created
September 3, 2013 01:03
-
-
Save mogutou1214/6418613 to your computer and use it in GitHub Desktop.
Careercup 2.7 - Implement a function to check if a linked list is a palindrome.
use a stack.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool isPalindrome(node *head){ | |
if(head==NULL) return false; | |
node *curr = head; | |
stack<int> s; | |
while(curr!=NULL){ | |
s.push(curr->data); | |
curr = curr->next; | |
} | |
/*while(!s.empty()){ | |
cout << s.top() << endl; | |
s.pop(); | |
} | |
*/ | |
curr = head; | |
while(curr!=NULL){ | |
if(s.top()!=curr->data) | |
return false; | |
curr = curr->next; | |
s.pop(); | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment