LeetCode 142. Linked List Cycle II
来源:互联网 发布:淘宝卖家版本 编辑:程序博客网 时间:2024/06/03 19:19
Linked List Cycle II
题目思路
判断给定链表中是否存在环,如果存在环返回环的起点。
参考这篇文章。
题目代码:
/** * 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 == nullptr || head->next == nullptr) return nullptr; ListNode *fast = head; ListNode *slow = head; while(fast->next && fast->next->next){ slow = slow->next; fast = fast->next->next; if(slow == fast){ fast = head; while(fast != slow){ fast = fast->next; slow = slow->next; } return fast; } } return nullptr; }};
阅读全文
0 0
- [LeetCode]141. Linked List Cycle【&142. Linked List Cycle II】
- leetcode--141. Linked List Cycle&&142. Linked List Cycle II
- LeetCode 141. Linked List Cycle && 142. Linked List Cycle II
- 142. Linked List Cycle II leetcode list
- LeetCode 142. Linked List Cycle II
- [LeetCode]142.Linked List Cycle II
- [leetcode] 142.Linked List Cycle II
- [leetcode] 142. Linked List Cycle II
- leetcode 142. Linked List Cycle II
- [Leetcode]142. Linked List Cycle II @python
- [LeetCode]142. Linked List Cycle II
- 142. Linked List Cycle II LeetCode
- LeetCode:142. Linked List Cycle II
- LeetCode--142. Linked List Cycle II
- LeetCode 142. Linked List Cycle II
- LeetCode 142. Linked List Cycle II
- LeetCode *** 142. Linked List Cycle II
- LeetCode 142. Linked List Cycle II
- Java设计模式之工厂模式(一):简单工厂模式+工厂方法模式
- Android 打开设置界面或者WiFi连接界面
- CORS跨域资源共享你该知道的事儿
- 【JavaWeb笔记】JavaScript(一) 基本知识
- Kotlin语言学习之Array数组使用
- LeetCode 142. Linked List Cycle II
- DIV+CSS弹性盒子布局
- js坐标和H5新增事件
- 手写渐变色
- SSH综合项目实战(快递) -- day13 角色数据管理、用户数据管理
- 【java学习笔记(零散知识点)】equals方法
- 在Mac上安装Linux系统
- idea导入eclipse项目工程,步骤详解
- JSP自定义标签查询数据