• 234. 回文链表


    链接

    思路:先把原来的链表复制一份,再将副本进行翻转,再逐一元素去比较翻转之后的副本和原链表是否相同。

    自己写的 C 代码:

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    // 先复制一份,再翻转,再比较是否相同
    bool isPalindrome(struct ListNode* head) {
        // 复制
        struct ListNode* copy = (struct ListNode*)malloc(sizeof(struct ListNode));
        copy->val = head->val;
        copy->next = NULL;
        struct ListNode* tail = copy;
        struct ListNode* tmp = head->next;
        while (tmp) {
            struct ListNode* new =
                (struct ListNode*)malloc(sizeof(struct ListNode));
            new->val = tmp->val;
            new->next = NULL;
            tail->next = new;
            tail = new;
            tmp = tmp->next;
        }
        // 翻转
        struct ListNode* prior = NULL;
        struct ListNode* current = copy;
        while (current) {
            struct ListNode* pnext = current->next;
            current->next = prior;
            prior = current;
            current = pnext;
        }
        // 现在翻转之后的链表的首元结点为 prior
        // 逐个比较
        while (prior != NULL && head != NULL) {
            if (prior->val != head->val) {
                return false;
            }
            prior = prior->next;
            head = head->next;
        }
        return true;
    }
    

    自己的思路比较繁琐。

    官方题解其中之一:

    思路:

    1. 复制链表值到数组列表中。
    2. 使用双指针法判断是否为回文。

    确定数组列表是否回文很简单,我们可以使用双指针法来比较两端的元素,并向中间移动。一个指针从起点向中间移动,另一个指针从终点向中间移动。这需要 O(n) 的时间,因为访问每个元素的时间是 O(1),而有 n 个元素要访问。

    C 代码:

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    bool isPalindrome(struct ListNode* head) {
        int vals[500001], vals_nums = 0;
        while (head != NULL) {
            vals[vals_nums++] = head->val;
            head = head->next;
        }
        for (int i = 0, j = vals_nums - 1; i < j; ++i, --j) {
            if (vals[i] != vals[j]) {
                return false;
            }
        }
        return true;
    }
    

    问题是这个数组要给的足够大,不然不能通过全部的测试用例。

  • 相关阅读:
    PostgreSQL 窗口函数汇总
    近似串匹配问题(动态规划)
    【校招VIP】java语言考点之双亲委派模型
    【CentOS 7】克隆虚拟机
    什么是MTU(Maximum Transmission Unit)?
    TensorFlow和Pytorch两种机器学习框架的比较及优缺点
    SveletJs学习——简介模块
    软件测试面试题:性能测试工作?
    selenium+python自动化安装驱动 碰到的问题
    javascript 深浅拷贝
  • 原文地址:https://blog.csdn.net/chengkai730/article/details/139766861