141. Linked List Cycle【easy】
141. Linked List Cycle【easy】
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) {
if (head == NULL || head->next == NULL) {
return false;
} ListNode * slow = head;
ListNode * fast = head; while (fast->next != NULL && fast->next->next != NULL) {
slow = slow->next;
fast = fast->next->next; if (fast == slow) {
return true;
}
} return false;
}
};
经典的快慢指针思想
141. Linked List Cycle【easy】的更多相关文章
- 141. Linked List Cycle【Easy】【判断链表是否存在环】
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 203. Remove Linked List Elements【Easy】【未排序链表删除其中的给定值】
Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...
- 102. Linked List Cycle【medium】
Given a linked list, determine if it has a cycle in it. Example Given -21->10->4->5, tail ...
- LeetCode--LinkedList--141.Linked List Cycle(Easy)
141. Linked List Cycle(Easy)2019.7.10 题目地址https://leetcode.com/problems/linked-list-cycle/ Given a l ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
随机推荐
- CodeForces - 283E Cow Tennis Tournament
Discription Farmer John is hosting a tennis tournament with his n cows. Each cow has a skill level s ...
- 前端基础-HTML标记语言
阅读目录 一. HTML标签与文档结构 二. HTML标签详细语法与注意点 三. HTML中标签分类 四. HTML注释 一. HTML标签与文档结构 HTML作为一门标记语言,是通过各种各样的标签来 ...
- STL之vector2
描述 依次输入n个整数,每次输入时检查该值是否已经出现在vector中,如果存在则不插入,否则将其插入到开头位置. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() ...
- 通俗解释遗传算法及其Matlab实现
早上再看一个APP推荐的文章,发现的. (1)初识遗传算法 遗传算法,模拟达尔文进化论的自然选择和遗传学机理的生物进化过程的计算模型,一种选择不断选择优良个体的算法.谈到遗传,想想自然界动物遗传是怎么 ...
- Oracle的日志记录模式
本篇摘自 http://www.cnblogs.com/cnjava/archive/2012/04/09/2439497.html --=============================== ...
- KVO(Key Value Observing)
*KVO能够监听某个对象属性的改变 原理:仅仅要给一个对象注冊一个监听,那么在执行时, 系统就会自己主动给该对象生成一个子类对象,而且重写自己主动生成的子类对象的被监听属性的set方法.然后在set方 ...
- java压缩 GZIP进行简单压缩,ZIP进行多文件保存
java压缩 GZIP进行简单压缩,ZIP进行多文件保存 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlhbmdydWkxOTg4/font/5a6 ...
- junit4单元測试总结
junit4单元測试总结 本文开发环境为myeclipse10.7 1. 准备工作 1.1. 选择须要单元測试的文件 创建mavenproject.右击须要单元測试的文件,选择New->oth ...
- HTML5 Canvas 绘制库存变化折线
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...
- .sh文件怎么安装?
实例:sh java_1.8.0.sh示例:sh filename.sh