【LeetCode】Linked List Cycle(环形链表)
这道题是LeetCode里的第141道题。
题目要求:
给定一个链表,判断链表中是否有环。
进阶:
你能否不使用额外空间解决此题?
简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast,一个slow。fast每一步前进两个节点,slow前进一个节点。判断fast和slow是否相等或者为空就行了。
贴个代码:
/**
* 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 *fast,*slow;
if(head==NULL||head->next==NULL)
return false;
fast=head->next;
slow=head;
while(fast!=slow){
if(fast->next!=NULL&&fast->next->next!=NULL){
fast=fast->next->next;
slow=slow->next;
}
else
return false;
}
return true;
}
};
贴个结果:

个人总结:
我最初的想法就是一个动指针一个定指针来循环判断是否为环,这个方法在时间上要比我之前的快。
【LeetCode】Linked List Cycle(环形链表)的更多相关文章
- [LeetCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- LeetCode 141. Linked List Cycle环形链表 (C++)
题目: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked ...
- [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 ...
- [LeetCode] Linked List Cycle II 链表环起始位置
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 141 Linked List Cycle 环形链表
给定一个链表,判断链表中否有环.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/problems/linked-list-cycle/description/ J ...
- LeetCode Linked List Cycle 单链表环
题意:给一个单链表,判断其是否出现环! 思路:搞两个指针,每次,一个走两步,另一个走一步.若有环,他们会相遇,若无环,走两步的指针必定会先遇到NULL. /** * Definition for si ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- Negut 上传乱码
解决办法 修改 bat 文件的 格式为ANSI格式即可
- Semi-prime H-numbers
题目描述形如4n+1的数被称为“H数”,乘法在“H数”集合内为封闭的.因数只有1和本身的数叫“H素数”(不包括1),其余叫“H合数”.一个“H合成数”能且只能分解为两个“H素数”.求0·h内的“H合成 ...
- HTTP 三次握手 建立连接 和 四次握手断开连接
三次握手建立连接 第一次握手:主机A发送位码为syn=1,随机产生seq number=1234567的数据包到服务器,主机B由SYN=1知道,A要求建立联机: 第二次握手:主机B收到请求后要确 ...
- write命令
write——给用户发信息,以Ctrl+D保存结束 命令所在路径:/usr/bin/write 示例1: # write xiaohua 执行命令后可以输入需要发送的信息,如下: 同时xiaohua收 ...
- Caused by: java.lang.ClassNotFoundException: org.springframework.boot.system.JavaVersion
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.system.JavaVersion Invalid pro ...
- 使用javap深入理解Java整型常量和整型变量的区别
我下图代码第五行和第九行分别定义了一个整型变量和一个整型常量: static final int number1 = 512; static int number3 = 545; Java程序员都知道 ...
- Codeforces Round #317 (Div. 2) D Minimization (贪心+dp)
D. Minimization time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Asp.Net Core 进阶(二) —— 集成Log4net
Asp.Net Core 支持适用于各种内置日志记录API,同时也支持其他第三方日志记录.在我们新建项目后,在Program 文件入口调用了CreateDefaultBuilder,该操作默认将添加以 ...
- python_112_网络编程 Socket编程
实例1:客户端发小写英文,服务器端返回给客户端大写英文(仅支持一次接受发送) 服务器端: #服务器端(先于客户端运行) import socket server=socket.socket() ser ...
- jExcelAPI导入导出excel
MS的电子表格(Excel)是Office的重要成员,是保存统计数据的一种常用格式.作为办公文档,势必要涉及到的电子文档的交换,Excel是一种在企业中非常通用的文件格式,打印和管理也比较方便.在 ...