您的当前位置:首页正文

L10.【LeetCode笔记】回文链表

来源:九壹网


1.题目

2.自解

一个容易想到的方法:将链表的每个节点的val存入一个数组中,最后用双指针访问

有关双指针判断回文链表的思想参见

代码

bool isPalindrome(struct ListNode* head) 
{
    int arr[100000]={0};
    int i=0; 
    struct ListNode* cur=head; 
    while (cur)
    {
        arr[i]=cur->val;
        i++;
        cur=cur->next;
    }
    int* left=&arr[0];
    int* right=&arr[i-1];
    while (left<right)
    {
        if (*left==*right)
        {
            left++;
            right--;
        }
        else
        return false;
   }
   return true;
}

提交结果

时间复杂度和空间复杂度均为

因篇幅问题不能全部显示,请点此查看更多更全内容

Top