leetcode——142——Linked List Cycle II
来源:互联网 发布:js正则判断数字范围 编辑:程序博客网 时间:2024/06/06 03:54
Given a linked list, return the node where the cycle begins. If there is no cycle, return
null
.Note: Do not modify the linked list.
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) {} * }; */ class Solution { public: int numNode = 0; ListNode *detectCycle(ListNode *head) { if (!hasCycle(head)) return NULL; ListNode* pBegin = head; ListNode* pEnd = head; for (int i = 0; i < numNode; i++) pBegin = pBegin->next; while (pBegin != pEnd) { pBegin = pBegin->next; pEnd = pEnd->next; } return pBegin; } bool hasCycle(ListNode *head) { if (head == NULL || head->next == NULL) return false; ListNode* fast = head; ListNode* slow = head; while (fast != NULL) { fast = fast->next; if (fast != NULL) { fast = fast->next; slow = slow->next; numNode++; } if (fast == slow) return true; } return false; } };
0 0
- LeetCode——Linked List Cycle II
- LeetCode——Linked List Cycle II
- leetcode——142——Linked List Cycle II
- leetcode 142 —— Linked List Cycle II
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- [LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- LeetCode——141. Linked List Cycle && 142. Linked List Cycle II
- LeetCode题解——Linked List Cycle II
- leetcode刷题日记—— Linked List Cycle II
- Leetcode——142. Linked List Cycle II
- LeetCode — Linked List Cycle II 解题报告
- LeetCode 之 Linked List Cycle I II — C++ 实现
- Algorithms—142.Linked List Cycle II
- LeetCode142—Linked List Cycle II
- LeetCode 142 — Linked List Cycle II(C++ Java Python)
- LeetCode——Linked List Cycle
- LeetCode——Linked List Cycle
- Redis+Spring缓存实例(windows环境,附实例源码及详解)
- 六、树和二叉树--(0)什么是树
- 安装并运行Elasticsearch
- 百练+BFS+图上visit标记嘛,因为是最短嘛,防止重复嘛
- SGU-495 Kids and Prizes(概率DP)
- leetcode——142——Linked List Cycle II
- 【DP】探索数字迷塔
- linux 硬盘分区,分区,删除分区,格式化,挂载,卸载笔记
- Android手机客户端与服务器端的信息通信交互
- yaf框架控制器中的几个函数
- visio2013产品密钥
- BZoj 1016: [JSOI2008]最小生成树计数【最小生成树】
- 城市中的原始人(二):本能大脑
- 百练+Trie数+求字符串前缀的经典模板