建网站软件最新,网站备案单位查询系统,数据分析师考试科目,天津票网网站234. 回文链表
给你一个单链表的头节点 head #xff0c;请你判断该链表是否为回文链表。如果是#xff0c;返回 true #xff1b;否则#xff0c;返回 false 。
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* L…234. 回文链表
给你一个单链表的头节点 head 请你判断该链表是否为回文链表。如果是返回 true 否则返回 false 。
/*** 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) {vectorint res;while(head!nullptr){res.emplace_back(head-val);headhead-next;}int i0;int jres.size()-1;while(ij){if(res[i]!res[j]){return false;}i;j--;}return true;}
};