判断链表中是否有环

来源: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. 牛客练习赛49 E 筱玛爱游戏 (线性基+博弈)

    链接:https://ac.nowcoder.com/acm/contest/946/E 来源:牛客网 筱玛爱游戏 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

  2. 北京师范大学第十五届ACM决赛-重现赛K Keep In Line ( 字符串模拟实现)

    链接:https://ac.nowcoder.com/acm/contest/3/K 来源:牛客网 Keep In Line 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 26214 ...

  3. mepg

    MPEG(Moving Picture Experts Group,动态图像专家组)

  4. git上拉项目

  5. 1.Linux命令行快捷键、Vim

    1. 命令终端的快捷键使用 ctrl+b 左移光标 ctrl+f 右移光标 ctrl+u 删除光标左边的内容 ctrl+k 删除光标右边的内容 ctrl+w 删除光标前的一个单词 =esc+ctrl+ ...

  6. SpringCloud学习系列-构建部门微服务消费者Module

    1.新建microservicecloud-consumer-dept-80 2.Pom <project xmlns="http://maven.apache.org/POM/4.0 ...

  7. 【hackerrank】Placements

    题目如下: You are given three tables: Students, Friends and Packages. Students contains two columns: ID  ...

  8. C#(.net)实现用apache activemq传递SQLite的数据

    版权声明:本文为搜集借鉴各类文章的原创文章,转载请注明出处:http://www.cnblogs.com/2186009311CFF/p/6382623.html. C#(.net)实现用apache ...

  9. PHP入门培训教程 php动态网页怎么转换成html

       当动态网页遇上搜索引擎 虽然动态网页相比于静态页面拥有许多优势,但它在搜索引擎的检索上却碰了个大钉子.无论任何一家网站,尤其是那些以营销为目的的企业网站,没有谁会希望自己的网页无法被搜索引擎检索 ...

  10. 小程序中css3实现优惠券

    效果如下: css3实现优惠券 知识储备 颜色渐变 linear-gradient() css伪类 :before :after index.wxss .app { /* padding: 20rpx ...