哈哈笔记之链表

来源:互联网 发布:淘宝恶意举报假货 编辑:程序博客网 时间:2024/06/07 04:05

写在前面:


我毕业了,现在充满信心的流窜于各大知名企业,手握自己dream企业的offer。。。是的,这是我的幻想!现在的我刚刚经历了一小波笔试面试的失败。和大多数人一样,从刚开始的充满信心,只看BAT,到现在是个机会都想去试一试,看一看,迫切想要把自己“嫁出去”。我甚至面试失败,私下给面试官发短息说我不要工资,让我当个实习生吧。面试官人很好,拒绝了我。。。我就知道自己的专业知识已经差到连实习生的要求都达不到。。。好!那我从头开始,把需要的专业知识尽可能补回来!来不及校招我就去当实习生,以后走社招!因此有了想要写学习笔记的想法,原因很多,不一一赘述,只想to be a better me...



题目描述:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.

Follow up:
Can you solve it without using extra space?



【思路】


class Solution {public:    ListNode *detectCycle(ListNode *head) {       ListNode *fast=head;       ListNode *slow=head;               do       {if((fast==NULL) || (fast->next==NULL))           return 0;        else             {              fast=fast->next->next;           slow=slow->next;        }               }while(fast!=slow);              slow=head;       while(slow!=fast){       fast=fast->next;       slow=slow->next;       }               return slow;            }};


原创粉丝点击