leetCode-linkedListCycle判断链表是否有环
2017-07-31 21:01
567 查看
题目
Given a linked list, determine if it has a cycle in it.Follow up:
Can you solve it without using extra space?
分析
判断链表是否有环,采用快慢指针,如果相遇则表示有环AC代码
/** * 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* slow = head; ListNode* fast = head->next; while(fast->next && fast->next->next && fast != slow){ fast = fast->next->next; slow = slow->next; } return fast == slow; } };
相关文章推荐
- LeetCode | Linked List Cycle(判断链表是否有环)
- leetcode解题之141# Linked List Cycle Java版 (判断链表是否有环)
- [leetcode 141] Linked List Cycle----判断链表是否为循环链表
- 【leetcode】Linked List Cycle II,判断链表是否有环
- [Leetcode] Linked list cycle ii 判断链表是否有环
- [Leetcode] Linked list cycle 判断链表是否有环
- [LeetCode-141] Linked List Cycle(判断链表是否有环)
- leetcode--Linked List Cycle--判断链表是否有环
- Palindrome Linked List - LeetCode 判断一个链表是否是回文结构
- 【Leetcode】234. Palindrome Linked List -判断链表是否为回文序列
- Linked List Cycle:判断链表是否有环
- 141 Linked List Cycle(判断链表是否有环Medium)
- 判断链表是否有环 Linked List Cycle
- linked-list-cycle——链表、判断是否循环链表、快慢指针
- Leetcode-234. Palindrome Linked List(判断链表是否回文)
- C实现 LeetCode->Linked List Cycle 双指针大法)(单链表是否有环)
- LeetCode 234. Palindrome Linked List判断链表是否回文
- LeetCode:Linked List Cycle--链表快慢指针实现求链表中位和查找链表中是否存在环
- [分析总结:leetcode-Palindrome Linked List] 给定单链表,判断链表是否为回文。
- 141.leetcode Linked List Cycle(easy)[链表是否有环 快慢指针]