题目

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

Follow up:

Can you solve it without using extra space?

分析

判断链表是否有环,采用快慢指针,如果相遇则表示有环

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

leetCode-linkedListCycle判断链表是否有环的更多相关文章

  1. LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java

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

  2. LeetCode 141. Linked List Cycle(判断链表是否有环)

    题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...

  3. [Leetcode] 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. Follow up:Can you solve it without using ext ...

  5. [Leetcode] Linked list cycle ii 判断链表是否有环

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

  6. 141 Linked List Cycle(判断链表是否有环Medium)

    题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...

  7. 判断链表是否有环(Java实现)

    判断给定的链表中是否有环.如果有环则返回true,否则返回false. 解题思路:设置两个指针,slow和fast,fast每次走两步,slow每次走一步,如果有环的话fast一定会追上slow,判断 ...

  8. 141. Linked List Cycle(判断链表是否有环)

    141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...

  9. python判断链表是否有环

    思路:使用快慢指针,快指针每次走两步,慢指针每次走一步,如果有环,则一定会快慢指针指向同一结点: 假设环的长度为n,先让一个指针走n步,另一个再开始走,当他们指针指向同一结点时,该结点就是环入口点 ( ...

随机推荐

  1. java发送get,post请求

    方法里面有注释:参照csdn里面的,项目用时自己改 package com.bst.express; import java.io.BufferedReader; import java.io.Dat ...

  2. bc https://en.wikipedia.org/wiki/Gossip_protocol

    分布式容错性:分布式网络极其鲁棒,能够容忍部分节点的异常状态: 不可篡改性:一致提交后的数据会一直存在,不可被销毁或修改: 隐私保护性:密码学保证了数据隐私,即便数据泄露,也无法解析. 随之带来的业务 ...

  3. [knownledge][latex] LaTex入门

    序言 最近需要写一份文档, 时间也不是特别紧. 之前一直用markdown写文档. 始终想学一下LaTex, 毕竟是学术论文界的工具. 在提及LaTex的内容之前. 事必是一定要首先提及高德纳的. 他 ...

  4. 读书笔记iOS-Core-Animation-Advanced-Techniques,iOS性能调试工具

    调试卡顿,除了使用timer profile,还可以使用 OpenGL ES驱动工具 OpenGL ES Driver工具显示的GPU利用率,打开Color Blended Layers 我们给图片和 ...

  5. 转:servlet的url-pattern匹配规则详细描述

    原文地址:servlet的url-pattern匹配规则详细描述   原文写的很详细 另外可以参考一下:Web.xml中设置Servlet和Filter时的url-pattern匹配规则 一.概述 在 ...

  6. Python摸爬滚打之day04----基本数据类型(列表,元组)

    1.列表 列表是可变的, 有序的数据类型,列表是按照添加顺序来保存的,可以存放各种数据类型. 1.1    列表的切片(同字符串) 1.2    列表的增删改查 注意: 列表是可以直接在列表上面进行操 ...

  7. ubuntu14.04下开启ssh服务

    1. 安装 sudo apt-get update sudo apt-get install openssh-server 2.开启服务 查看查看ssh服务是否启动 打开"终端窗口" ...

  8. 基于external version进行乐观锁并发控制

    ?version=1?version=1&version_type=external它们的唯一区别在于,_version,只有当你提供的version与es中的_version一模一样的时候, ...

  9. html多文件上传,可支持预览

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. BeanFactoryPostProcessor vs BeanPostProcessor

    BeanFactoryPostProcessors affect BeanDefinition objects because they are run right after your config ...