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. 连接到CentOS(Linux)服务器ssh、mysql缓慢

    现象: 服务器163与服务器164在同一机柜,双绞线直接连接,从办公室或者服务器163去连机服务器164的ssh.mysql均缓慢,让机房人员查了,无果.而164却正常. 最后发现两个机器/etc/r ...

  2. Linux在目录中查找某个函数

    1,在某个路径下查文件. 在/etc下查找“*.log”的文件 find /etc -name “*.log” 2,扩展,列出某个路径下所有文件,包括子目录. find /etc -name “*” ...

  3. html a 链接标签title属性换行鼠标悬停提示内容的换行效果

    鼠标经过悬停于对象时提示内容(title属性内容)换行排版方法,html title 换行方法总结. html的title属性默认是显示一行的.如何换行呢? 这里DIVCSS5总结介绍两种换行方法为大 ...

  4. Spark随笔(三):straggler的产生原因

    首先,介绍前辈研究的基于MapReduce框架的outlier产生原因:其次,根据这些方面来分析Spark架构中的straggler:最后,根据阅览的优化办法,谈谈自己的看法. 一.MapReduce ...

  5. javascript笔记7-事件

    主要讲事件流.事件捕获.事件冒泡.事件处理程序.事件属性.事件类型.内存和优化等. 由于本文已经在微信订阅号上发布,为了防止原创性冲突检测,因此本文在此处已经删除. 详细请扫描订阅号二维码,查看历史信 ...

  6. ssh连接linux服务器只显示-bash-4.1#不显示路径解决方法

    ssh连接linux服务器只显示-bash-4.1#不显示路径时,我们只需要修改  ~/.bash_profile文件,如果不存在这个文件,那么新建一个,增加内容  export PS1='[\u@\ ...

  7. E - 滑雪

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Pract ...

  8. Java-->服务器的响应(Servlet--doGet&doPost)

    --> Servelet: 用于接收请求(客户端,浏览器),做出响应的,服务器端的,java类 --> ServletLogin -- Web项目服务器响应的Java实现 package ...

  9. 一个共享内存hash

    Background 我们的多进程程序碰到一个需求:做key-value查询,然后拿获取到的value去做一些事情.这些key-value存储在很多词典文件中,数量级>10w,如果每个进程都加载 ...

  10. pycharm安装

    license server http://idea.lanyus.com