判断链表中是否有环

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

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

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

Java

  1. /**
  2. * Definition for singly-linked list.
  3. * class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) {
  7. * val = x;
  8. * next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. public boolean hasCycle(ListNode head) {
  14. if(head == null || head.next == null) {
  15. return false;
  16. }
  17. ListNode low = head.next, fast = head.next.next;
  18. while(fast != null && fast.next != null && low != fast) {
  19. low = low.next;
  20. fast = fast.next.next;
  21. }
  22. if(low == fast && low != null) {
  23. return true;
  24. }
  25. return false;
  26. }
  27. }

Python

  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. class Solution(object):
  7. def hasCycle(self, head):
  8. """
  9. :type head: ListNode
  10. :rtype: bool
  11. """
  12. if head == None or head.next == None:
  13. return False
  14. fast = head.next
  15. low = head
  16. while fast != None and fast.next != None:
  17. if fast == low:
  18. return True
  19. fast = fast.next.next
  20. low = low.next
  21. 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.

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

  1. /**
  2. * Definition for singly-linked list.
  3. * class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) {
  7. * val = x;
  8. * next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. public ListNode detectCycle(ListNode head) {
  14. if(head == null || head.next == null) {
  15. return null;
  16. }
  17. ListNode low = head.next, fast = head.next.next;
  18. while(fast != null && fast.next != null && low != fast) {
  19. low = low.next;
  20. fast = fast.next.next;
  21. }
  22. fast = head;
  23. while(low != null && low != fast) {
  24. low = low.next;
  25. fast = fast.next;
  26. }
  27. return low;
  28. }
  29. }

Python

  1. # -*- coding:utf-8 -*-
  2. # class ListNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. class Solution:
  7. def EntryNodeOfLoop(self, pHead):
  8. # write code here
  9. if pHead == None:
  10. return pHead
  11. fast = pHead
  12. slow = pHead
  13. while fast and fast.next:
  14. slow = slow.next
  15. fast = fast.next.next
  16. if fast == slow:
  17. fast = pHead
  18. while fast:
  19. if fast == slow:
  20. return fast
  21. fast = fast.next
  22. slow = slow.next
  23. 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. Codeforces Round #430 (Div. 2) - D

    题目链接:http://codeforces.com/contest/842/problem/D 题意:定义Mex为一个序列中最小的未出现的正整数,给定一个长度为n的序列,然后有m个询问,每个询问给定 ...

  2. jquery.fancybox.js 解决只加载一次的问题

    问题描述:有一块图片区域,页面第一次加载的时候jQuery(".fancybox-button").fancybox({}); 使用ajax上传成功后显示在图片区域,再次jQuer ...

  3. C#基础知识之GC 垃圾回收

    一.托管 .Net所指的托管资源到底是什么意思呢?是相对于所有资源,还是只限于某一方面的资源?很多人对此不是很了解. 其实.Net所指的托管只是针对内存这一个方面,并不是对于所有的元素:因此对于Str ...

  4. 洛谷P4003 [国家集训队2017]无限之环 网络流 最小费用最大流

    题意简述 有一个\(n\times m\)棋盘,棋盘上每个格子上有一个水管.水管共有\(16\)种,用一个\(4\)位二进制数来表示当前水管向上.右.下.左有个接口.你可以旋转除了\((0101)_2 ...

  5. LOJ6279 果树

    我丢 之前sun在某校集训给我看过 当时也没想起来 今天补省集的锅的时候发现 wok这题我还听过?! 身败名裂.jpg (可是你记性不好这事情不已经人尽皆知了吗? 咳咳 回归正题 考虑对于两个同色的点 ...

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

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

  7. python代码执行bash命令 -- python3 cook book

    python代码执行bash命令相关 -- python3 cook book refer: https://python3-cookbook.readthedocs.io/zh_CN/latest/ ...

  8. 【JavaScript】包装类

    包装类 String().Number().Boolean() String() 可以将基本数据类型的字符串转换为String对象 var string = new String("hell ...

  9. 【Java】使用@Valid+BindingResult进行controller参数校验

    @Valid @Valid注解用于校验,所属的包: javax.validation.Valid. 你可以定义实体,在实体的属性上添加校验规则,在API接收数据时添加@Valid注解,这时你的实体将会 ...

  10. Nginx 在 Linux 下安装与搭建集群

    搭建集群图例 集群搭建图如下,为了简单一点,使用一个Nginx服务器+两个Tomcat服务器,省略数据库部分: 环境说明 Linux 为 CentOS 7.2 发行版 + Java jdk 1.8 + ...