LeetCode(141)(142) Linked List Cycle I II
2015-08-09 16:43
609 查看
Linked List Cycle 代码
Linked List Cycle II 代码
[code]/** * 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) { set<ListNode *> visited; ListNode *p1; p1 = head; while(p1 != NULL) { if(visited.count(p1)) return true; else visited.insert(p1); p1 = p1->next; } return false; } };
Linked List Cycle II 代码
[code]/** * 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) { set<ListNode *> visited; ListNode *p1; p1 = head; while(p1 != NULL) { if(visited.count(p1)) return p1; else visited.insert(p1); p1 = p1->next; } return NULL; } };
相关文章推荐
- hdu 3593
- windowsphone8.1学习笔记之应用数据(二)
- hdu 3593 分类: hdu 2015-08-09 16:43 12人阅读 评论(0) 收藏
- LeetCode(20)Valid Parentheses
- leetcode_242——Valid Anagram (字典法)
- 第三周工作周报
- java synchronized静态同步方法与非静态同步方法,同步语句块
- ListView上拉加载更多
- Java回顾之I/O
- php框架laravel学习 二 (数据库建立迁移与建模)
- UVA 152 Tree's a Crowd
- 二叉搜索树的基本操作(查找、插入、删除)【数据结构】
- LGPL 与GPL的区别
- SEO在网页制作中的应用
- Java io文件操作(按字符、字节、行、随机读取,追加,文件操作)
- ubuntu更换阿里源
- HDU 5366-The mook jong(组合数学)
- java中classpath的详解
- STL学习笔记--序列式容器
- 沐雪微信2.0最新版本升级啦