Leetcode Linked List Cycle II
来源:互联网 发布:双十一大学生网购数据 编辑:程序博客网 时间:2024/04/29 22:37
题意:判断链表中是否右环, 如果有则输出环的起始点, 如果没有则输出NULL。
思路:记录链表的地址,用hash表实现O(1)查找。
/** * 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) { if(head == NULL) return head; map<ListNode*, bool> ml; ListNode* next = head; while(next && !ml[next]) { ml[next] = true; next = next->next; } return next; }};
如何实现空间的O(1)还需思考。
0 0
- 【LeetCode】 Linked List Cycle II
- Leetcode: Linked List Cycle II
- <Leetcode>Linked List Cycle II
- [LeetCode] Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode:Linked List Cycle II
- Leetcode Linked List Cycle II
- [LeetCode]Linked List Cycle II
- leetcode Linked List Cycle II
- LeetCode | Linked List Cycle II
- Leetcode: Linked List Cycle II
- [LeetCode] - Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode - Linked List Cycle II
- leetcode Linked List Cycle II
- Linked List Cycle II -- LeetCode
- LeetCode Linked List Cycle II
- [LeetCode] Linked List Cycle II
- IO流File的迭代
- 【寒江雪】Direct3DX11索引缓存
- C++ PRIMER PLUS 第六版编程答案(三)
- Leetcode Sum Root to Leaf Numbers
- JavaScript之HTML DOM
- Leetcode Linked List Cycle II
- javascript dom simpledemo
- 说说UIViewController那些事儿(下)
- 3Sum
- Leetcode Reorder List
- Leetcode Binary Tree Postorder Traversal
- Git 远程操作
- FaceBook面试题 Implement strStrp(String a, String b)
- Leetcode Kth Smallest Element in a BST