234.回文链表(LeetCode)

简介: 234.回文链表(LeetCode)

想法一

先找中间结点,如果为偶数,则指向第二个结点,再把后半段逆置,最后从头进行比对


f39a4609697a9069bba683d487d7b437_9badca45d58b4d41b4c51a37b671a646.png


中间节点:


这段代码在找中间结点那题写过,所以直接拷贝过来


876.链表的中间结点(LeetCode)-CSDN博客


struct ListNode* middleNode(struct ListNode* head)
{
    //要求只遍历一遍链表
    struct ListNode* slow = head;//一次走一格
    struct ListNode* fast = head;//一次走两格
    while (fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (!fast)
        {
            break;
        }
    }
    return slow;
}


逆置:


这段代码也在反转链表时写过,所以直接拷贝过来


206.反转链表(LeetCode)-CSDN博客


struct ListNode* reverseList(struct ListNode* head)
{
    struct ListNode* cur = head;
    struct ListNode* rhead = NULL;
    while (cur)
    {
        //头插
        struct ListNode* next = cur->next;
        cur->next = rhead;
        rhead = cur;
        cur = next;
    }
    return rhead;
}


最后,比对才是本题需要做的事情。如果head结点的值与rmid不相等,则不是回文,如果相等,则同时往后一个结点,如果循环结束还没return false,那就证明链表是回文的,return true


bool isPalindrome(struct ListNode* head)
{
    struct ListNode* mid = middleNode(head);
    struct ListNode* rmid = reverseList(mid);
    while (rmid)
    {
        if (head->val != rmid->val)
        {
            return false;
        }
        else
        {
            head = head->next;
            rmid = rmid->next;
        }
    }
    return true;
}


可能有小伙伴会担心,是奇数个节点的时候,好像head比rmid少一个节点啊?其实,不用担心,因为rmid前的节点还是指向结尾的,如图

1f8d393f4702e2650eba26a07a2cf184_bc7de6dfdce2460db857b54df4abeb38.png


所以最后两个指针会共同走到最后一个节点


总结,这是一道糅合了之前多种方法(可以偷懒,cv大法)的题


完整代码:


struct ListNode* reverseList(struct ListNode* head)
{
    struct ListNode* cur = head;
    struct ListNode* rhead = NULL;
    while (cur)
    {
        //头插
        struct ListNode* next = cur->next;
        cur->next = rhead;
        rhead = cur;
        cur = next;
    }
    return rhead;
}
struct ListNode* middleNode(struct ListNode* head)
{
    //要求只遍历一遍链表
    struct ListNode* slow = head;//一次走一格
    struct ListNode* fast = head;//一次走两格
    while (fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (!fast)
        {
            break;
        }
    }
    return slow;
}
bool isPalindrome(struct ListNode* head)
{
    struct ListNode* mid = middleNode(head);
    struct ListNode* rmid = reverseList(mid);
    while (rmid)
    {
        if (head->val != rmid->val)
        {
            return false;
        }
        else
        {
            head = head->next;
            rmid = rmid->next;
        }
    }
    return true;
}
相关文章
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
|
15天前
【力扣】409.最长回文串
【力扣】409.最长回文串
|
15天前
【力扣】21. 合并两个有序链表
【力扣】21. 合并两个有序链表
|
2月前
|
存储 JavaScript
leetcode82. 删除排序链表中的重复元素 II
leetcode82. 删除排序链表中的重复元素 II
22 0
|
2月前
leetcode83. 删除排序链表中的重复元素
leetcode83. 删除排序链表中的重复元素
10 0
|
2月前
leetcode2807.在链表中插入最大公约数
leetcode2807.在链表中插入最大公约数
16 0
|
2月前
leetcode2487.从链表中移除节点
leetcode2487.从链表中移除节点
20 1
|
2月前
|
存储 算法
LeetCode刷题--- 61. 旋转链表(快慢指针+闭合为环)
LeetCode刷题--- 61. 旋转链表(快慢指针+闭合为环)
|
2月前
|
算法 索引
LeetCode刷题--- 138. 复制带随机指针的链表(哈希表+迭代)
LeetCode刷题--- 138. 复制带随机指针的链表(哈希表+迭代)
|
2月前
|
算法
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)