142环形链表II Posted on 2020-05-13 Words count in article: 144 | Reading time ≈ 1 1234567891011121314151617181920212223242526272829303132333435/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *detectCycle(ListNode *head) { ListNode *cur1 = head ;//cur1 是快指针 ListNode *cur2 = head;//cur2 是慢指针 if(!head||!head->next)//如果头节点就是空,那就直接输出false return NULL; while(cur1&&cur1->next) { cur1 = cur1->next->next; cur2 = cur2->next; //相等的时候,我们进行快节点指向头部,慢节点不动,然后一起走的操作 if(cur1==cur2) { cur1 = head; while(cur1!=cur2) { cur1= cur1->next; cur2=cur2->next; } return cur2; } } return NULL; }}; -------------本文结束,感谢您的阅读------------- Post author: Jason Post link: https://jasonxqh.github.io/2020/05/13/142%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.