剑指offer专项突击版第7天

剑指 Offer II 021. 删除链表的倒数第 n 个结点
简单链表预处理长度,也可以用栈 或 双指针( i i i 先走 n n n 个,然后 i i i j j j 一起走直到 i i i 到空指针)来做

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        int sz = 0;
        ListNode res(0,head), *t = head;
        while(t){
            sz++; 
            t = t->next;
        }
        t = head; 
        ListNode *tmp = &res;
        for(int i = 0; i < sz-n; i++, t = t->next, tmp = tmp->next) ;
        tmp->next = t->next;
        return res.next;
    }
};

剑指 Offer II 022. 链表中环的入口节点
哈希表做法

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *t = head, flag(0);
        unordered_map<ListNode*,bool> um;
        while(t) {
            if(!um.count(t)) um[t] = true;
            else return t;
            t = t->next;
        }
        return nullptr;
    }
};

快慢指针做法
f a s t fast fast 走两步, s l o w slow slow 走一步,直到 f a s t fast fast s l o w slow slow 相遇说明链表存在环。
在相遇时,通过数学关系得到 s l o w slow slow 到环入口点+n圈环的距离刚好等于链表头到环入口点距离

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *fast = head, *slow = head;
        while(fast) {
            slow = slow->next;
            if(fast->next == nullptr) return nullptr;
            fast = fast->next->next;
            if(fast == slow) {
                auto ptr = head;
                while(ptr != slow) {
                    ptr = ptr->next;
                    slow = slow->next;
                }
                return ptr;
            }
        }   
        return nullptr;     
    }
};

剑指 Offer II 023. 两个链表的第一个重合节点

  1. t1走完A,走B
  2. t2走完B,走A

情况一、不相交
他们最后都会同时到nullptr而结束循环。
情况二、相交
最后同时到达相交点

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode *t1 = headA, *t2 = headB;
        while(t1 != t2){
            t1 = t1 == nullptr? headB: t1->next;
            t2 = t2 == nullptr? headA: t2->next;
        }
        return t1;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值