【easy】141. Linked List Cycle
非常简单的题:判断链表有没有环(用快慢指针)
/**
* 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的更多相关文章
- 【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 ...
- 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...
- 【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 ...
- 【LeetCode】142. Linked List Cycle II
Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...
- 【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 ...
- 【一天一道LeetCode】#141. Linked List Cycle
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode❤python】141. Linked List Cycle
#-*- coding: UTF-8 -*- #Method:快慢指针法,建立虚表头,快指针走两步,慢指针走一步,若存在环,则快指针会追上慢指针# Definition for singly-link ...
- 【Lintcode】103.Linked List Cycle II
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- 【Lintcode】102.Linked List Cycle
题目: Given a linked list, determine if it has a cycle in it. Example Given -21->10->4->5, ta ...
随机推荐
- Java的properties文件读取和属性修改
package Test; import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileO ...
- 洛谷P2756 飞行员配对方案问题
二分图裸题,找他的最大匹配即可 #include<bits/stdc++.h> using namespace std; int n,m,ans; ; int to[N]; struct ...
- vi设置行号
首先,我们先打开一个文件,用vim 文件名 就可以直接使用vim打开 我们事先写了一些内容在这个txt里面,我们可以看到如下内容 在这里,我们可以直接敲命令, :set number 或者 ...
- 浅析Java的Object类
前言: 最近在回顾Java基础,在此过程中,查看源码是少不了的 这里以JDK8以基准,记录一些自己查看源码的观感 Object类,翻阅源码,看看这个类的所在位置,是在 java.lang 包下 ...
- 一道B树的题目---先记一下, 还没看到B树
D
- 仿 ELEMENTUI 实现一个简单的 Form 表单
原文:仿 ElmentUI 实现一个 Form 表单 一.目标 ElementUI 中 Form 组件主要有以下 功能 / 模块: Form FormItem Input 表单验证 在这套组件中,有 ...
- Fedora 24系统基本命令
Fedora 24基本命令 一. DNF软件管理 1. 修改配置:在/etc/dnf/dnf.conf中加入fastestmirror=true.keepcache=true ...
- noi.ac89A 电梯
题目 思路 首先按照\(t\)排序!!!! 首先考虑一个暴力\(dp\) 用\(f[i]\)表示前\(i\)个人到达地点所需要的时间. 那么就有如下的转移 \[f_i = min_{1 \le j \ ...
- windows下网络编程UDP
转载 C++ UDP客户端服务器Socket编程 UDPServer.cpp #include<winsock2.h>#include<stdio.h>#include< ...
- 《Linux下cp XXX1 XXX2的功能》的实现
<Linux下cp XXX1 XXX2的功能>的实现 一.题目要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyC ...