Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
Posted 洞拐洞幺
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. 相关的知识,希望对你有一定的参考价值。
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
Follow up:
class Solution { public: ListNode *detectCycle(ListNode *head) { if(head == NULL){ return 0; } ListNode* slow = head; ListNode* fast = head; while(fast != NULL && fast->next != NULL){ slow = slow->next; fast = fast->next->next; if(slow == fast){ break; } } if(fast == NULL || fast->next == NULL){ return NULL; } slow = head; while(slow != fast){ slow = slow->next; fast = fast->next; } return slow; } };
以上是关于Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. 的主要内容,如果未能解决你的问题,请参考以下文章
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
A linked list is given such that each node contains an additional random pointer which could point t
problem report: middle of linked list