Linked List Cycle
来源:互联网 发布:instagram拍照软件好吗 编辑:程序博客网 时间:2024/06/03 23:14
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ #include<set>class Solution {public: bool hasCycle(ListNode *head) { set<ListNode*> listSet; listSet.insert(head); ListNode* p = head; while(p&&p->next) { if(listSet.find(p->next)==listSet.end()){ listSet.insert(p->next); p=p->next; }else{ return true; } } return false; }};
class Solution {public: bool hasCycle(ListNode *head) { ListNode *slow = head,*fast = head; while(fast&&fast->next) { slow = slow->next; fast = fast->next->next; if(slow==fast) return true; } return false; }};
0 0
- leetcode Linked List Cycle & Linked List Cycle ||
- Linked List Cycle II Linked List Cycle
- 【LeetCode】Linked List Cycle
- Leetcode: Linked List Cycle
- Leetcode Linked List Cycle
- Linked List Cycle
- Linked List Cycle II
- Linked List Cycle
- Linked List Cycle
- Linked List Cycle II
- Leetcode: Linked List Cycle
- LeetCode:Linked List Cycle
- Linked List Cycle II
- Linked List Cycle
- Linked List Cycle II
- [LeetCode]Linked List Cycle
- Linked List Cycle
- LeetCode | Linked List Cycle
- c++ 判断字符串只包含字母、数字、汉字
- javascript__基础表单
- HDU 2579 - Dating with girls(2)
- Codeforces 437C The Child and Toy(贪心)
- Interview
- Linked List Cycle
- 关于使用Javascript来模拟多线程的一点想法
- kernfs_addrm_start kernfs_add_one
- 给你的网站添加访问统计和事件统计
- Your Progress As A Programmer Is All Up To You
- HDU 1163 Eddy's digital Roots(九余数定理)
- java并发编程(8)--callable&Future
- 关于java面试
- 机器人控制入门的一些想法(2)