判断链表中是否有环

来源:https://leetcode.com/problems/linked-list-cycle

Given a linked list, determine if it has a cycle in it.

一块一慢两个指针,如果有环,两个指针必定会在某个时刻相同且都不为空

Java

/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null || head.next == null) {
return false;
}
ListNode low = head.next, fast = head.next.next;
while(fast != null && fast.next != null && low != fast) {
low = low.next;
fast = fast.next.next;
}
if(low == fast && low != null) {
return true;
}
return false;
}
}

Python

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if head == None or head.next == None:
return False
fast = head.next
low = head
while fast != None and fast.next != None:
if fast == low:
return True
fast = fast.next.next
low = low.next
return False

找到链表中环的起点

来源:https://leetcode.com/problems/linked-list-cycle-ii

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

快慢两个指针相遇时,快指针从头开始一步遍历,慢指针从相遇节点一步遍历,下次相遇的结点就是环的入口节点

/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null || head.next == null) {
return null;
}
ListNode low = head.next, fast = head.next.next;
while(fast != null && fast.next != null && low != fast) {
low = low.next;
fast = fast.next.next;
}
fast = head;
while(low != null && low != fast) {
low = low.next;
fast = fast.next;
}
return low;
}
}

Python

# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def EntryNodeOfLoop(self, pHead):
# write code here
if pHead == None:
return pHead
fast = pHead
slow = pHead
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if fast == slow:
fast = pHead
while fast:
if fast == slow:
return fast
fast = fast.next
slow = slow.next
return None

Linked List Cycle(链表成环)的更多相关文章

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

  2. [LeetCode] 142. Linked List Cycle II 链表中的环 II

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

  3. [LeetCode] Linked List Cycle II 单链表中的环之二

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

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

  5. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  6. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  7. 【题解】【链表】【Leetcode】Linked List Cycle II

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

  8. LeetCode之“链表”:Linked List Cycle && Linked List Cycle II

    1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...

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

  10. (链表 双指针) leetcode 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

随机推荐

  1. [AI]cognitive business|cognitive thinking

    just watch the cognitive business presentation of IBM CEO Ginni. here is my thoughts in this field : ...

  2. u-boot Kconfig结构解析

    1 /u-boot-2019.07/Kconfig source "arch/Kconfig" 2 /u-boot-2019.07/arch/Kconfig  -->有一个一 ...

  3. Java 学习输入1234 求和

    输入任何整数都能求和 import java.util.Scanner; /** * @author 作者anil E-mail:888@yirose.com * @date 创建时间:2017年3月 ...

  4. JavaSE---泛型系统学习

    1.概述 1.1.泛型: 允许在   定义  类.接口.方法时  使用  类型形参,这个类型形参  将在声明变量.创建对象.调用方法时  动态地指定: 1.2.jdk5后,引入了  参数化类型(允许程 ...

  5. Vue刷新token,判断token是否过期

    1.判断token是否过期,前端请求后,后台会返回一个状态给你.根据状态判断是否过期,刷新token 2.是否每次请求后端都会返回新的token给你.或者后端给你定义了一个刷新token的方法,那此时 ...

  6. 花式赋值、列表、字典、解压缩、input()、格式化学习笔记

    目录 花式赋值 列表(list) 字典(dict) 解压缩 input()与用户交互 格式化的三种方式 f_String格式化(important) %s.%d占位符 format 格式化(不常用) ...

  7. Linux学习-samba服务

    一.Samba介绍 Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成. SMB (Server Messages Block,信息服务块) 是一种在局域网 ...

  8. iOS---实现在屏幕上实时绘图的简单效果---CAShaperLayer和UIBezierPath的简单运用

    首先,声明几个属性 @property(nonatomic,strong)UIBezierPath * beizer; @property(nonatomic,assign)CGPoint start ...

  9. 前端面试题常考&必考之--盒子模型和box-sizing(项目中经常使用)

    主要考察width的值,包括padding\border\content等属性??? box-sizing属性是css3特有的哦*** 1>当box-sizing:content-box;时,跟 ...

  10. 洛谷 P2590 BZOJ 1036 [ZJOI2008]树的统计

    Time limit 10000 ms//另外,BZOJ只算所有点的总时限,所以可能会放过一些原本会TLE的代码 Memory limit 165888 kB OS Linux SourceZJOI2 ...