[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 extra space?
给定一个链表,判断是否有环存在。Follow up: 不使用额外空间。
解法:双指针,一个慢指针每次走1步,一个快指针每次走2步的,如果有环的话,两个指针肯定会相遇。
Java:
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
}
Python:
class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
# @param head, a ListNode
# @return a boolean
def hasCycle(self, head):
fast, slow = head, head
while fast and fast.next:
fast, slow = fast.next.next, slow.next
if fast is slow:
return True
return False
C++:
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
};
类似题目:
[LeetCode] 142. Linked List Cycle II 链表中的环 II
All LeetCode Questions List 题目汇总
[LeetCode] 141. Linked List Cycle 链表中的环的更多相关文章
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 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 ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- [LeetCode] 141. Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- 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 141. Linked List Cycle
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- 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 ...
- [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 ...
随机推荐
- Rendering in UE4
Intro Thinking performance. Identify the target framerate, aim your approach on hitting that target ...
- LGOJP2051 [AHOI2009]中国象棋
比较明显的计数dp.不知道为什么被打了状压的tag... 不难发现无论炮放在哪里其实是等价的,需要知道的只有这一列放了一个炮还是两个炮还是还没放,那么可以设\(f[i,j,k]\)表示第\(i\)行, ...
- 回调方式进行COM组件对外消息传递
情景:被调用者--COM组件:调用者---外部程序作用:COM组件 到 外部程序 的消息传递方法: 1.外部程序通过接口类对象,访问接口类的方法.COM对象通过连接点方式,进行消息的反向传递. 2.外 ...
- easyui 自己写的一些小东西
1设置combobox,当我们只需要显示一个commbobox的时候,并且默认选择第一项 function Getcombobox(comboId, value, groupNo) { $('#' + ...
- 从输入URL到页面返回的过程详解
文章转自以为大神的博客;https://www.cnblogs.com/xianyulaodi/p/6547807.html#_labelTop 总结的很不错,看完收获颇多, 下面就是大神的文章,我只 ...
- C++定义和初始化数组以及memset的使用(转)
一.一维数组 静态 int array[100]; 定义了数组array,并未对数组进行初始化 静态 int array[100] = {1,2}: 定义并初始化了数组array 动态 int* ar ...
- L1025
1,对于搜索,我有一个不成熟的想法,这不就是,强化版的for循环吗? 2,反正是搜索,那就先找搜索状态, n,x,n是第几次分,x是分剩下的数. 3,这个我觉得自己努力努力可能可以做出来. 4,首先要 ...
- MySQL 为什么不用分区表(转载)
一分钟系列 潜在场景如何? 当MySQL单表的数据量过大时,数据库的访问速度会下降,“数据量大”问题的常见解决方案是“水平切分”. MySQL常见的水平切分方案有哪些? (1)分库分表: (2)分区表 ...
- vue中代理实现方法
vue中代理实现方法如下: const path = require('path'); function resolve(dir) { return path.join(__dirname, dir) ...
- nexus 3.17.0 做为golang 的包管理工具
nexus 3.17.0 新版本对于go 包管理的支持是基于go mod 的,同时我们也需要一个athens server 然后在nexus 中配置proxy 类型的repo 参考配置 来自官方的配置 ...