LeetCode 234. 回文链表
思路:
将链表数据存入vector用双指针解决
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
vector<int> a;
while (head) {
a.push_back(head ->val);
head = head ->next;
}
int i = 0, j = a.size() - 1;
while (i < j) {
if (a[i] != a[j]) {
return false;
}
i ++;
j --;
}
return true;
}
};

浙公网安备 33010602011771号