题目

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

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

代码

用hashmap版

/**
* 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) {
std::map<ListNode *, bool> ifNodeOccured;
ListNode *p = head;
while ( p )
{
if ( ifNodeOccured.find(p) != ifNodeOccured.end() ) return true;
ifNodeOccured.insert(std::pair<ListNode *, bool>(p,true));
p = p->next;
}
return false;
}
};

不用hashmap,改用双指针技巧版

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

Tips

这两种解法都是基本的技巧。第二种双指针的技巧效率更高,且空间复杂度更低,似乎得到了更多的推崇;但个人总觉得hashmap的方法不错,而且不强依赖于技巧。

==============================================

第二次过,用双指针技巧过的。第一次没有AC,因为忘记了dummpy.next = head这句;第二次AC了。

/**
* 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 dummpy(-);
dummpy.next = head;
ListNode* p1 = &dummpy;
ListNode* p2 = &dummpy;
while ( p2 )
{
p1 = p1->next;
p2 = p2->next;
if ( !p2 ) return false;
p2 = p2->next;
if ( p1==p2 ) return true;
}
return false;
}
};

【Linked List Cycle】cpp的更多相关文章

  1. leetcode 【 Linked List Cycle 】 python 实现

    题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...

  2. 【Linked List Cycle II】cpp

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

  3. leetcode 【 Linked List Cycle II 】 python 实现

    公司和学校事情比较多,隔了好几天没刷题,今天继续刷起来. 题目: Given a linked list, return the node where the cycle begins. If the ...

  4. 【Reverse Linked List II】cpp

    题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...

  5. 【Insertion Sorted List】cpp

    题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...

  6. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

  7. 【Search Insert Position 】cpp

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  8. 【First Missing Positive】cpp

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  9. 【Merge Sorted Array】cpp

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

随机推荐

  1. RING3到RING0

    当我在说跳转时,说的什么? CPU有很多指令,不是所有的指令都能够随时用,比如 ltr指令就不是随便什么时候能用,在保护模式下,如果你不安规则来执行指令,CPU就会抛出异常,比如你在INTEL手册上就 ...

  2. 3D向2D投影

    http://blog.sina.com.cn/s/blog_536e0eaa0100jn7j.html

  3. LeetCode Single Number III (xor)

    题意: 给一个数组,其中仅有两个元素是出现1次的,且其他元素均出现2次.求这两个特殊的元素? 思路: 跟查找单个特殊的那道题是差不多的,只是这次出现了两个特殊的.将数组扫一遍求全部元素的异或和 x,结 ...

  4. LeetCode Longest Substring Without Repeating Characters 最长不重复子串

    题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...

  5. http头部信息学习

    想的每2周进行知识的总结,自己拖延症有犯了,发现自己知识库量还是太少,平时总结和发现问题还不够深,对待问题的深度也存在很多问题,但是坚持学习,总结,后面应该会有收获, 1.常见的返回码 100: 请服 ...

  6. spark 之knn算法

    好长时间忙的没写博客了.看到有人问spark的knn,想着做推荐入门总用的knn算法,顺便写篇博客. 作者:R星月  http://www.cnblogs.com/rxingyue/p/6182526 ...

  7. kafka 开机启动脚本

    /etc/init.d$ vi kafka-start-up.sh #!/bin/bash #export KAFKA_HOME=$PATH export KAFKA_HOME=/opt/Kafka/ ...

  8. python_29_三级菜单

    menu={ '北京':{ '海淀':{ '五道口':{ '搜狐':{}, '网易':{}, 'Google':{}, }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, '优酷':{}, ...

  9. Java环境变量搭建(Linux环境)

    1. 下载解压JDK压缩包 例如:解压到 /opt/jdk1.7.0_80 下 2. 添加环境变量到 /etc/profile 文件中 vi /etc/profile 在文件末尾追加如下内容: exp ...

  10. C#获取Honeywell voyager 1400g扫码后的数据

    一.在类方法中加入 System.IO.Ports.SerialPort com;二.在构造方法中加入 try {   com = new System.IO.Ports.SerialPort(&qu ...