linked-list-cycle
Posted 修修55
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linked-list-cycle相关的知识,希望对你有一定的参考价值。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCycle(ListNode *head) { if(!head||!head->next) return false; ListNode *fast = head; ListNode *slow = head; while(fast && fast->next){ slow = slow->next; fast = fast->next->next; if(fast == slow) return true; } return false; } };
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
以上是关于linked-list-cycle的主要内容,如果未能解决你的问题,请参考以下文章
linked-list-cycle——链表判断是否循环链表快慢指针