【Lintcode】103.Linked List Cycle II
题目:
Given a linked list, return the node where the cycle begins.
If there is no cycle, return null
.
Given -21->10->4->5
, tail connects to node index 1,return 10
题解:
Solution 1 ()
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if (!head) {
return head;
} ListNode* slow = head;
ListNode* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
break;
}
}
if (!fast || !fast->next) {
return nullptr;
} slow = head;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
} return slow;
}
};
【Lintcode】103.Linked List Cycle II的更多相关文章
- 【LeetCode】142. Linked List Cycle II (2 solutions)
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- 【LeetCode】142. Linked List Cycle II
Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...
- 【Lintcode】102.Linked List Cycle
题目: Given a linked list, determine if it has a cycle in it. Example Given -21->10->4->5, ta ...
- 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...
- 【LeetCode】141. Linked List Cycle (2 solutions)
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- 【LeetCode】141. Linked List Cycle
题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...
- 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...
- 【easy】141. Linked List Cycle
非常简单的题:判断链表有没有环(用快慢指针) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
- 【Lintcode】364.Trapping Rain Water II
题目: Given n x m non-negative integers representing an elevation map 2d where the area of each cell i ...
随机推荐
- 邮箱大师WPZ协议包
WIRELESS Z PACKET: i8-version(WZPUnit.getVersion() & 3 | WZPUnit.MAGIC_MASK = 1 & 3 | -48 = ...
- C#里类的get和set方法编写和调用
using System; class Date { int day; int month; int year; public int Day{ get { return day; } set { d ...
- Mac修改默认python版本
研究python爬虫,需要用到Beautiful Soup 但是Mac默认的python版本为2.7 自己安装了3.6的版本 import 报错 查找资料: Mac在启动,会先加载系统配置文件(包括~ ...
- JavaScript -- JavaScript DOM 编程艺术(第2版)
/* 渐进增强 平稳退化 网页 结构层(structural layer): HTML 表示层(presentation layer): CSS <link rel="styleshe ...
- os如何处理键盘的所有按键,显示or不显示,显示是如何显示
[0]README 0.1) source code and text decription are from orange's implemention of a os , and for comp ...
- 转载 ----Android学习笔记 - 蓝牙篇 (Bluetooth)
1.什么是蓝牙 Bluetooth是目前使用的最广泛的无线通讯协议之一 主要针对短距离设备通讯(10米) 常用于连接耳机.鼠标和移动通讯设备等 2.发现周围蓝牙设备 BluetoothAd ...
- linux 读取文件信息并且输出
版权为个人所有,欢迎转载如转载请说明出处.(东北大亨) http://www.cnblogs.com/northeastTycoon/p/5513231.html 以下为读取文件信息做输出操作. 1. ...
- iOS视频直播用到的协议
一 .流媒体 1 - 伪流媒体 1.1 扫盲:边下载边播放1.2 伪流媒体:视频不是实时播放的,先把视频放在数据库,再供客户端访问,比如:优酷,爱奇艺等 1.3 特点: 边下边存,文件会保存.遵守了 ...
- Upgrading Elasticsearch
Upgrading Elasticsearch | Elasticsearch Reference [5.6] | Elastic https://www.elastic.co/guide/en/el ...
- BZOJ2539: [Ctsc2000]丘比特的烦恼
BZOJ2539: [Ctsc2000]丘比特的烦恼 Description 随着社会的不断发展,人与人之间的感情越来越功利化. 最近,爱神丘比特发现,爱情也已不再是完全纯洁的了. 这使得丘比特很是苦 ...