Palindrome Linked List
Problem
Given the head
of a singly linked list, return true
if it is a palindrome.
Solution Approach
Click - to see solution code
- C++
class Solution {
public:
bool isPalindrome(ListNode* head) {
vector<int> a, b;
while (head) {
a.push_back(head->val);
head = head->next;
}
b = a;
reverse(b.begin(), b.end());
if (a == b) return true;
return false;
}
};