Skip to content

Instantly share code, notes, and snippets.

@kalyco
Created July 6, 2018 21:59
Show Gist options
  • Save kalyco/7a3b16bfdfe381b725cdf74fb6a1e9b0 to your computer and use it in GitHub Desktop.
Save kalyco/7a3b16bfdfe381b725cdf74fb6a1e9b0 to your computer and use it in GitHub Desktop.
Check if the linked list is a palindrome
// Linked List Question
// Check if the linked list is a palindrome
// set a node variable starting with mhead.
// Iterate through first half of LL
// append elements to a string
// if after first half, starting with last elem in array,
// compare to the first node. if != return false
// inc n each time.
// after loop, if still valid, return true
bool function palindrome() {
string a1[mSize/2];
node n = mHead;
for(int i=0; i<mSize; i++) {
if (i<(mSize/2)) {
a.append(n.value);
} else {
if (a[(mSize/2 - i)] == n.data) return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment