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;
    }
};
posted @ 2022-08-26 10:08  hjy94wo  阅读(15)  评论(0)    收藏  举报