Linked List Cycle

Given a linked list, determine if it has a cycle in it.

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:
bool hasCycle(ListNode *head) {
ListNode *p1, *p2;
p1 = p2 = head;
while(p2 && p2->next && p2->next->next) {
p2 = p2->next->next;
p1 = p1->next;
if(p1 == p2) return true;
}
return false;
}
};

Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Follow up: Can you solve it without using extra space?

说明:在上题基础上,将一个指针放到链表头,步长都设为1,相遇节点。(可以计算)

class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *p1, *p2;
p1 = p2 = head;
while(p2 && p2->next && p2->next->next) {
p2 = p2->next->next;
p1 = p1->next;
if(p1 == p2) {
p1 = head;
while(p1 != p2) {
p1 = p1->next;
p2 = p2->next;
}
return p1;
}
}
return NULL;
}
};

15. Linked List Cycle && Linked List Cycle II的更多相关文章

  1. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  2. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  3. [Linked List]Linked List Cycle,Linked List Cycle II

    一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...

  4. Linked List Cycle && Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...

  5. LeetCode之“链表”:Linked List Cycle && Linked List Cycle II

    1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...

  6. [Linked List]Reverse Linked List,Reverse Linked List II

    一.Reverse Linked List  (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...

  7. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  8. LeetCode之旅(15)-Odd Even Linked List

    题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...

  9. 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements

    237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...

随机推荐

  1. (转)android平台phonegap框架实现原理

    (原文)http://blog.csdn.net/wuruixn/article/details/7405175 android平台phonegap框架实现原理 分类: Android2012-03- ...

  2. rpm软件包管理

    RPM 有五种基本的操作方式(不包括创建软件包): 安装, 卸载, 升级, 查询,和验证. 下面我们就来逐一的讲解吧. 一. 安装RPM包 RPM 软件包通常具有类似foo-1.0-1.i386.rp ...

  3. WindowsPhone开发—— 使用手绘图片做景区导览地图

    前些日子在做景区App遇到需求,使用手绘图片做一个简易的地图,支持放大缩小平移以及显示景点Mark,安卓上可以使用一个叫做“mAppWidget”的开源库来完成,WP上有人建议用ArcGIS,但是考虑 ...

  4. 一行代码从表中选取N行到另一个表

    private void Form1_Load(object sender, EventArgs e) {            DataTable dt = new DataTable();     ...

  5. Linux TC流量控制HOWTO中文版

    <本文摘自Linux的高级路由和流量控制HOWTO中文版 第9章节>网人郭工进行再次编译: 利用队列,我们可以控制数据发送的方式.记住我们只能对发送数据进行控制(或称为整形).其实,我们无 ...

  6. LintCode Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...

  7. 有100个节点的AVL树最大深度是多少?

    首先说AVL树的概念 1 左右子树的深度差<=1 2 左右子树都是AVL树. 其实这样算,可以倒推的. 空树  DEPTH = 0; AVL_DEPTH = 2^0+2^1+......+2^k ...

  8. 【转载】WebDriver常用的鼠标/键盘操作

    注:driver为一个WebDriver的实例,xpath为一个元素的xpath字符串,在本文中一律采用xpath的方式定位元素 1.鼠标右键点击操作:Actions action = new Act ...

  9. C语言基本数据类型

    一.数据类型与“模子” short.int.long.char.float.double 这六个关键字代表C 语言里的六种基本数据类型. 怎么去理解它们呢? 举个例子:见过藕煤球的那个东西吧?(没见过 ...

  10. mysql出现启动不了问题

    查询日志后是‘ Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist’此错误(less ...