当前位置: 代码迷 >> 综合 >> LeetCode:回文链表(c++)
  详细解决方案

LeetCode:回文链表(c++)

热度:99   发布时间:2024-03-08 09:18:31.0

题目描述:

请判断一个链表是否为回文链表。

示例 1:

输入: 1->2
输出: false

示例 2:

输入: 1->2->2->1
输出: true
class Solution {
public:bool isPalindrome(ListNode* head) {vector<int> vec;ListNode* cur =head;while(cur){vec.push_back(cur->val);cur=cur->next;}for( int i=0,j=vec.size()-1;i<j;i++,j--){if(vec[i]!=vec[j])return false;}return true;}
};