一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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 = head;//慢指针
        ListNode* p2 = head;//快指针
        while(p1!=NULL&&p2!=NULL&&p2->next!=NULL)//如果没有环,总会指向NULL
        {
            p1=p1->next;
            p2=p2->next->next;
            if(p1==p2) return true;//慢指针追上快指针,表示成环
        }
        return false;
    }
};

【一天一道LeetCode】#141. Linked List Cycle的更多相关文章

  1. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  2. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

  3. [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 ext ...

  4. [LeetCode] 141. Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  5. 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 ext ...

  6. leetcode 141 Linked List Cycle Hash fast and slow pointer

    Problem describe:https://leetcode.com/problems/linked-list-cycle/ Given a linked list, determine if ...

  7. 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 ext ...

  8. Java for 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 ex ...

  9. leetcode 141. Linked List Cycle ----- java

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

  10. Python [Leetcode 141]Linked List Cycle

    题目描述: Given a linked list, determine if it has a cycle in it. 解题思路: 快的指针和慢的指针 代码如下: # Definition for ...

随机推荐

  1. Ubuntu 16.04 LTS(入门一)国内快速更新软件源

    一.源文件位置 备份并替换/etc/apt/sources.list的源内容: 二.更改源文件内容 sudo gedit /etc/apt/sources.list deb http://mirror ...

  2. 什么是spool系统,什么是预输入,什么是缓输出?

    操作系统提供外部设备联机同时操作的功能设备spool系统,又称为假脱机系统. spool系统在应用程序执行前将应用程序的信息通过独占设备预先输入到辅存上的一个特定的存储区域存放好.称为预输入. 在应用 ...

  3. Linux下用程序实现统计cpu和内存的利用率

    Linux下没有直接可以调用系统函数知道CPU占用和内存占用.那么如何知道CPU和内存信息呢.只有通过proc伪文件系统来实现. proc伪文件就不介绍了,只说其中4个文件.一个是/proc/stat ...

  4. ref string

    string pics=""; pSub.GetSubjectContent(pddm,ref  pics); public string GetSubjectContent(st ...

  5. 字符流之FileReader&FileWriter

    package zhang; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; pub ...

  6. RandomAccessFile&IO流&排序&方法论

    RandomAccessFile&IO流&排序&方法论 我们总觉得历史是极其遥远的东西,与我们并无关联,又觉得历史隐藏在图书馆的旧书之中. 然而,我们每个人都有真真切切的历史. ...

  7. Linux 查看CPU温度

    安装 lm-sensors sudo apt-get install lm-sensors # 安装yes | sudo sensors-detect # 侦测所有感测器 sensors # 查看温度 ...

  8. dephi FillChar 的几种写法

    //在 delphi 新版中, char 已经是双字节了.故应该重新自己写一个函数,取名为 FillByte ,才无歧义. procedure TForm1.Button2Click(Sender: ...

  9. PHP 实例 AJAX 与 XML

    在 PHP 中,AJAX 可用来与 XML 文件进行交互式通信,具体的通信过程,请参考本文内容! AJAX XML 实例 下面的实例将演示网页如何通过 AJAX 从 XML 文件读取信息: 实例   ...

  10. JavaScript 注释

    JavaScript 注释可用于提高代码的可读性. JavaScript 注释 JavaScript 不会执行注释. 我们可以添加注释来对 JavaScript 进行解释,或者提高代码的可读性. 单行 ...