Linked List Cycle(链表成环)
判断链表中是否有环
来源: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(链表成环)的更多相关文章
- [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] 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 ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [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 ...
- [算法][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 ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- 【题解】【链表】【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 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 ...
- 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 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 ...
随机推荐
- PAT Advanced 1002 A+B for Polynomials (25 分)(隐藏条件,多项式的系数不能为0)
This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...
- GUI学习之二十二——QRubberBand学习总结
今天学习一种全新的输入控件——QRubberBand()控件(橡皮筋选中) 一.描述 QRubberBand()提供了一个矩形或西安来只是选择或边界的效果(就像在桌面上点击鼠标后拖拽拉出来的框一样), ...
- MySQL新增用户及赋予权限
创建用户 USE mysql; #创建用户需要操作 mysql 表 # 语法格式为 [@'host'] host 为 'localhost' 表示本地登录用户,host 为 IP地址或 IP 地址区间 ...
- Spring Boot 使用Mybatis注解开发增删改查
使用逆向工程是遇到的错误 错误描述 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): c ...
- Java动手动脑02
一.平方数静方法: public class SquareInt { public static void main(String[] args) { int result; for (int x = ...
- apply()的使用
apply()方法的另一种使用,就是可以代替一次for循环 在封装 document.getElementsByTagName的时候,需要把伪数组转为一个真的数组,其是也不是真的数组 typeof 出 ...
- linux运维、架构之路-Kubernetes集群部署
一.kubernetes介绍 Kubernetes简称K8s,它是一个全新的基于容器技术的分布式架构领先方案.Kubernetes(k8s)是Google开源的容器集群管理系统(谷歌内部 ...
- Ubuntu 16.04下使用docker部署rabbitmq
(以下docker相关的命令,需要在root用户环境下或通过sudo提升权限来进行操作.) 1.拉取rabbimq镜像到本地 docker pull rabbitmq 2. Docker运行rabbi ...
- 【bzoj3564】 [SHOI2014]信号增幅仪
题目描述: 无线网络基站在理想状况下有效信号覆盖范围是个圆形.而无线基站的功耗与圆的半径的平方成正比. 现给出平面上若干网络用户的位置,请你选择一个合适的位置建设无线基站.... 就在你拿起键盘准备开 ...
- int转字符串 stringstream
1. 设定一个任意数字串,数出这个数中的偶数个数,奇数个数,及这个数中所包含的所有位数的总数,将答案按 “偶-奇-总” 的位序,排出得到新数.重复进行,最后会得到 123. #include<i ...