题目

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. Spring MVC的测试

    测试是保证软件质量的关键. 与 Spring MVC 相关的测试,主要涉及控制器的测试. 为了测试Web项目通常不需要启动项目,需要一些Servlet相关的一些模拟对象,比如MockMVC.MockH ...

  2. Cocos2d-x v3.1 GUI系统--环境构建(七)

    Cocos2d-x v3.1 GUI系统--环境构建(七) 在使用Cocos2d-x的GUI系统时,由于生成的工程默认是没有将GUI系统所需的库导入到项目的,所以我们必须把库导入到工程中并对工程做一些 ...

  3. Markdown引用图片,且不使用网上链接的解决方法

    首先介绍下markdown使用图片的3种方法 使用本地图片,缺点是要用到本地的绝对路径,不适合对文档做迁移,否则会有图片链接失效的情况 ![thisisimage](C:\\Users\\Goose\ ...

  4. LeetCode Valid Anagram (简单题)

    题意: 给出两个字符串s和t,判断串t是否为s打乱后的串. 思路: 如果返回的是true,则两个串的长度必定相等,所有字符出现的次数一样.那么可以统计26个字母的次数来解决,复杂度O(n).也可以排序 ...

  5. 常用宏OC

    #ifndef MacroDefinition_h #define MacroDefinition_h //-------------------获取设备大小--------------------- ...

  6. centos下的安装mysql,jdk

    mysql: 如果你是用rpm安装, 检查一下RPM PACKAGE:rpm -qa | grep -i mysql如果mysql已经安装在本机,则会列出mysql安装过的文件 ,像mysql-ser ...

  7. POJ-3080 Blue Jeans---字符串+暴力

    题目链接: https://vjudge.net/problem/POJ-3080 题目大意: 找最长的公共字串(长度>=3),长度相同就找字典序最小的 解题思路: 枚举第一个串的所以子串,处理 ...

  8. Java jvm 内存回收机制

    http://blog.csdn.net/yaerfeng/article/details/51291903 在Java中,它的内存管理包括两方面:内存分配(创建Java对象的时候)和内存回收,这两方 ...

  9. mkfs.xfs 命令找不到的解决方法

    对硬盘进行格式化: # mkfs.xfs /dev/sdb1 系统显示: mkfs.xfs error: command not found. 可能是系统不完全安装 运行 which mkfs  查看 ...

  10. 【转】操作系统Unix、Windows、Mac OS、Linux的故事

    电脑,计算机已经成为我们生活中必不可少的一部分.无论是大型的超级计算机,还是手机般小巧的终端设备,都跑着一个操作系统.正是这些操作系统,让那些硬件和芯片得意组合起来,让那些软件得以运行,让我们的世界在 ...