力扣hot100学习记录(八)

206. 反转链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
在这里插入图片描述
题意
给一个链表,将链表进行翻转
思路

  • 用两个指针维护相邻两个点,每次把后面一个点指向前一个点,直到后一个点指向空,最后把第一个节点指向空。
  • 也可以递归处理,把head->next翻转,然后把head->next节点指向head,head->next置为空。
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(!head) return NULL;
        auto a = head, b = head->next;
        while(b){
            auto c = b->next;
            b->next = a;
            a = b;
            b = c;
        }
        head->next = NULL;
        return a;
    }
};
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(!head || !head->next) return head;
        auto tail = reverseList(head->next);
        head->next->next = head;
        head->next = NULL;
        return tail;
    }
};

234. 回文链表

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。
在这里插入图片描述
题意
判断一个链表是否为回文链表
思路
先将链表后一半进行翻转,然后用两个指针从头尾遍历,判断对应节点的值是否相等,不等则不是回文链表,最后把链表还原。
代码

class Solution {
public:
    bool isPalindrome(ListNode* head) {
        int n = 0;
        for(auto i = head;i; i = i->next) n++;
        if(n<=1) return true;
        int half = n / 2;
        auto a = head;
        for(int i = 0; i < n - half; i++) a = a->next;
        auto b = a->next;

        for(int i = 0; i < half - 1;i++){
            auto c = b->next;
            b->next = a;
            a = b;
            b = c;
        }

        auto p = head, q = a;
        bool success = true;
        for(int i = 0; i < half; i++){
            if(p->val != q->val){
                success = false;
                break;
            }
            p = p->next;
            q = q->next;
        }

        auto tail = a;
        b = a->next;
        for(int i = 0; i < half - 1; i++){
            auto c = b->next;
            b->next = a;
            a =b, b = c;
        }
        tail->next = NULL;
        return success;
    }
};
  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值