【LeetCode】234. 回文链表

前言

为了养成创作习惯,从今天起开始刷力扣了,只刷简单和中等难度的题,分类刷,这个月就先刷链表题,从简单的开始按着顺序刷,一天一道,养生做法。

为了锻炼一下自己,决定用 C 来写,据说是受苦,不过一天一道无所谓了。

题目描述

234. 回文链表 - 力扣(LeetCode)

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回  true ;否则,返回  false 。

示例 & 提示

示例 1:

<pre class="prettyprint hljs lua" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">输入:head = [1,2,2,1]
输出:true
复制代码</pre>
示例 2:
<pre class="prettyprint hljs lua" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">输入:head = [1,2]
输出:false
复制代码</pre>
提示:
<pre class="hljs makefile" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">[1, 105]
0 <= Node.val <= 9
</pre>

思路

  • 暴力就是把链表值复制到一个数组里,然后双指针比较

  • 首先我们需要找到中间结点,然后将后面的结点逆置,前段和后段对比即可

  • 又或者我们可以换个思路,比如在找中间结点的时候逆置前半部分?这样如果是奇数个元素,需要多判断一下

  • 最后还有递归做法,我们都知道可以用递归反向打印链表,思路这不就有了

具体代码

<pre class="prettyprint hljs elixir" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">bool isPalindrome(struct ListNode *head) {
	if (head == NULL || head->next == NULL) {
        return true;
    }

    struct ListNode *slow = head, *fast = head, *pre, *tmp;
    while (fast != NULL && fast->next != NULL) {
        slow = slow->next;
        fast = fast->next->next;
    }

    pre = slow;
    slow = slow->next;
    pre->next = NULL;
    while (slow != NULL) {
        tmp = slow->next;
        slow->next = pre;
        pre = slow;
        slow = tmp;
    }

    while (pre != NULL) {
        if (head->val != pre->val) {
            return false;
        }
        pre = pre->next;
        head = head->next;
    }
    return true;
}
复制代码</pre>
<pre class="prettyprint hljs php" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">bool isPalindrome(struct ListNode *head) {
    if (head == NULL || head->next == NULL) {
        return true;
    }

    struct ListNode *slow = head, *fast = head, *pre = NULL, *cur;
    while (fast != NULL && fast->next != NULL) {
        cur = slow;
        slow = slow->next;
        fast = fast->next->next;

        cur->next = pre;
        pre = cur;
    }

    if (fast != NULL) {
        slow = slow->next;
    }

    while (cur != NULL) {
        if (cur->val != slow->val) {
            return false;
        }
        cur = cur->next;
        slow = slow->next;
    }
    return true;
}
复制代码</pre>
<pre class="prettyprint hljs rust" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">struct ListNode *tmp;
bool check(struct ListNode *head) {
    if (head == NULL) {
        return true;
    }
    bool res = check(head->next) && (tmp->val == head->val);
    tmp = tmp->next;
    return res;
}

bool isPalindrome(struct ListNode *head) {
    tmp = head;
    return check(head);
}</pre>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要验链表是否为回文链表,可以采用以下方法: 1. 使用快慢指针找到链表的中点。 2. 将链表的后半部分反转。 3. 将链表的前半部分与反转后的后半部分进行逐一比较,如果存在不相等的节点,则该链表不是回文链表。 4. 如果链表的所有节点都比较完毕,且没有找到不相等的节点,则该链表回文链表。 具体操作步骤如下: 1. 初始化快慢指针,快指针每次移动两个节点,慢指针每次移动一个节点,直到快指针到达链表末尾。此时慢指针指向链表的中点。 2. 将链表的后半部分反转。可以使用一个新的指针从慢指针位置开始反转,每次将当前节点的next指针指向前一个节点,然后将指针向后移动一个节点,直到到达链表末尾。最后将反转后的链表的头节点保存下来。 3. 分别使用两个指针,一个指向链表的头节点,另一个指向反转后的链表的头节点。逐一比较两个指针指向的节点的值是否相等,如果存在不相等的节点,则该链表不是回文链表。 4. 如果链表的所有节点都比较完毕,且没有找到不相等的节点,则该链表回文链表。 通过以上方法,我们可以检验链表是否为回文链表。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++数据结构与算法之判断一个链表是否为回文结构的方法](https://download.csdn.net/download/weixin_38737980/14866827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [判断链表是否为回文链表leetcode-LeetCodeAlgorithm:算法问题](https://download.csdn.net/download/weixin_38660295/19945543)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [判断链表是否为回文链表leetcode-LeetCode:力码](https://download.csdn.net/download/weixin_38600696/19945643)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值