非常简单的题:判断链表有没有环(用快慢指针)

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

【easy】141. Linked List Cycle的更多相关文章

  1. 【LeetCode】141. Linked List Cycle (2 solutions)

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

  2. 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...

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

  4. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  5. 【LeetCode】142. Linked List Cycle II (2 solutions)

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  6. 【一天一道LeetCode】#141. Linked List Cycle

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 【leetcode❤python】141. Linked List Cycle

    #-*- coding: UTF-8 -*- #Method:快慢指针法,建立虚表头,快指针走两步,慢指针走一步,若存在环,则快指针会追上慢指针# Definition for singly-link ...

  8. 【Lintcode】103.Linked List Cycle II

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

  9. 【Lintcode】102.Linked List Cycle

    题目: Given a linked list, determine if it has a cycle in it. Example Given -21->10->4->5, ta ...

随机推荐

  1. java学习——递归

    /** * 添加商品类型的功能 * 注意创建时间和修改时间在具体的方法中直接赋值 * @param gT 商品类型管理表映射的GT类的实例化对象 */ @Override public void ad ...

  2. [转帖]创建文件或修改文件时间 touch

    Linux命令(五)创建文件或修改文件时间 touch https://www.cnblogs.com/ay-a/p/7900274.html touch -t .x86_64.rpm 记得 wind ...

  3. 排序—时间复杂度为O(n2)的三种排序算法

    1 如何评价.分析一个排序算法? 很多语言.数据库都已经封装了关于排序算法的实现代码.所以我们学习排序算法目的更多的不是为了去实现这些代码,而是灵活的应用这些算法和解决更为复杂的问题,所以更重要的是学 ...

  4. c提高第四次作业

    1. 简述指针数组和数组指针的区别?答: 指针数组:是一个数组,每个元素都是指针 数组指针:是一个指针,指向数组的指针 2. 如何定义一个指向 int a[10] 类型的指针变量(数组指针)(使用3种 ...

  5. XPath、CSS 选择器 -学习地址

    http://www.w3school.com.cn/cssref/css_selectors.asp http://www.w3school.com.cn/xpath/xpath_syntax.as ...

  6. C语言函数-strcat

    strcat: 将两个char类型连接. char d[20]="GoldenGlobal"; char *s="View"; strcat(d,s); 结果放 ...

  7. Oracle查询所有表的字段明细

    SELECT USER_TAB_COLS.TABLE_NAME as 表名, UTC.COMMENTS as 表中文名, USER_TAB_COLS.COLUMN_ID as 列序号, USER_TA ...

  8. [BJOI2019]排兵布阵(动态规划)

    [BJOI2019]排兵布阵(动态规划) 题面 洛谷 题解 暴力dp: 设\(f[i][j]\)表示考虑到了第\(i\)座城市用了\(j\)人的最大收益,枚举在这个城市用多少人就可以了. 优化: 发现 ...

  9. Kubernetes fluentd+elasticsearch+kibana

    前提:dns服务,k8s集群 下载kubernetes,并解压 https://github.com/kubernetes/kubernetes/releases tar zxvf kubernete ...

  10. unet 网络接受任意大小的输入

    将网络的输入定义的placeholder 大小设为:[None,None,c], 不设置固定的大小. 但是出现问题: 前层特征图与后层特征图进行组合时,尺寸大小不一致: [32, 60, 256] 和 ...