`
betakoli
  • 浏览: 166594 次
社区版块
存档分类
最新评论

LeetCode 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?

题意:如何判断链表是否有环

这是一个经典的问题,后面还有追问:

如果有环,计算环的长度?

找到碰撞点的位置?

 

首先,如何找环,设计两个指针,分别从链表的头节点开始,走一部和走两步,循环一直到走两步指针为空(说明没有环)或者两指针相遇(说明有环)为止。

主要代码:

/**
 * 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)
    		return false;
        ListNode p1,p2;
        p1=head;
        p2=head;
        boolean ifcycle = false;
        while(p2.next!=null&&p2.next.next!=null)
        {
        	p1=p1.next;
        	p2=p2.next.next;
        	if(p1==p2)
        	{
        		ifcycle = true;
        		break;
        	}
        }
        return ifcycle;
    }
}

 如何计算环的长度:如下图:



 第一次相遇时,走两步的指针总共路程为: S(两步)=圆周+S1+S2 ;走一步的指针路程为 S(一步)=S1+S2 ; 所以环长度为 S(两步)-S(一步)

 

如何找到碰撞点的位置:

由于走两步指针的路程为走一步指针所走路程的两倍,S(两步)=2(S1+S2),再根据第二个公式,可以得出S(圆周)= S1+S2 ; 在这个公式中S2这段是重复的,也就是说从碰撞点开始继续向前走到环入口点的距离和从链表头结点走到入口点的距离是相等的。我们只需要设定两个指针分别从头结点和碰撞点开始每次走一步,相遇点就是环入口点。

  • 大小: 5.4 KB
分享到:
评论

相关推荐

    leetcode中文版-LeetCode:力码

    leetcode中文版 LeetCode/Cpp 本人刷题记录在此,包含题意理解与算法思路,包含在Cpp文件内部注释,后续会持续更新。 有不懂的可以联系ji648513181,同时也欢迎志同道合O的朋友一起合作更新。 已更新剑指Offer答案...

    leetcode不会-Leetcode-Java:Leetcode-Java

    leetcode 不会 Leetcode Solutions in Java Linked List Linked List Cycle Given a linked list, determine if it has a cycle in it. public static boolean hasCycle(ListNode head) 快慢指针法,块指针从head....

    leetcode怎么销号-LeetCode-Top-Interview-Questions:LeetCode-Top-Interview-Qu

    leetcode怎么销号 LeetCode便签 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? 解决思路 声明一个慢指针和一个快...

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    lrucacheleetcode-LeetCode:LeetCode刷题

    leetcode LeetCode 剑指offer LeetCode解题记录(python) 2018.9.19 两数之和(Two Sum) 2018.9.19 两数相加(Add Two Numbers) 2018.9.25 翻转二叉树(Invert Binary Tree) 2018.9.25 环形链表(Linked List ...

    leetcode答案-leetcode-java:leetcode的Java代码

    com.leetcode.list Linked List Cycle Linked List Cycle II Remove Duplicates from Sorted List com.leetcode.string Single Number com.leetcode.tree Balanced Binary Tree Maximum Depth of Binary Tree Same ...

    leetcode2sumc-LeetCode:LeetCode的一些题目

    leetcode 2 sum c LeetCode 帮助文档 ...Cycle 160 Easy Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List

    javalruleetcode-leetcode-java:力码笔记

    leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring Without Repeating Characters 5.Longest Palindromic Substring 20.Valid Parentheses 26.Remove Duplicates from Sorted ...

    leetcode中325题python-leetcode:leetcode

    leetcode中325题python leetcode 以 参考 和 Hash相关 1_两数之和 ...linked-list-cycle-ii 143 重排链表 reorder-list 148 排序链表 sort-list 234 回文链表 palindrome-linked-list 双指针遍历/滑动

    java二叉树算法源码-algorithm-primer:algorithmprimer-算法基础、Leetcode编程和剑指offer,Ja

    算法基础、Leetcode编程、剑指offer 目录 Leetcode编程 Leetcode Category 栈与队列 No Problem Solution Difficulty Tag 20 有效的括号 Valid Parentheses Easy 94 二叉树的中序遍历 Binary Tree Inorder Medium ...

    leetcode卡-leetcode:利特码解决方案

    Cycle trees Convert Sorted Array to Binary Search Tree string and search First Bad Version Dynamic Programing *** Climbing Stairs Set Matrix Zeroes API System.arrayCopy 刷题顺序 TOP100 其中算法,主要...

    tech.github.io:我的博客

    终生成长 :hot_beverage: 为什么要建这个仓库 梳理自己掌握的知识点,整理自己的知识体系。 I Hear and I Forget, I See and I ... Linked List CycleLeetcode 21. Merge Two Sorted ListsLeetCode 224. Basic Cal

    LeetCode:LeetCode解决方案

    preorder-traversal链表reorder-list链表linked-list-cycle-ii链表linked-list-cycle动态规划word-break-ii动态规划word-break链表copy-list-with-random-pointer复杂度single-number-ii复杂度single-number动态规划

    leetcode中国-leetcode:刷算法了

    leetcode中国 Leetcode Set Matrix Zeroes 第一种 通过一次遍历记录所有为0的索引(Python中enumerate()输出当前列表的索引) 再遍历一次, 根据记录的索引进行置0 第二种 通过一次遍历所有为0的索引, 设置当前索引的...

    leetcode和oj-leetcode_downloader:用于您已接受的leetcodeoj提交的下载器

    linked-list-cycle/Solution.993783.java $ tree . ├── add-binary │  └── Solution.665166.java ├── add-two-numbers │  └── Solution.666385.java ├── balanced-binary-tree │  └── ...

    leetcode2sumc--Offer:-提供

    leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类...Cycle 160 * Intersection of Two Linke

    leetcode2sumc-ack-CherishLeetCode:ack-CherishLeetCode

    leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类...Cycle 160 * Intersection of Two Linke

    leetcode分发糖果-ForDaFactory:使用C++的个人leetcode解决方案

    141-环形链表:linked-list-cycle 142-环形链表:linked-list-cycle-ii 160-相交链表:intersection-of-two-linked-lists 206-反转一个单链表:reverse-linked-list 20-有效的括号:valid-parentheses 150-逆波兰表达式求...

    leetcode答案-LeetCodeSolution:这是LeetCode网站问题的解决方案集

    Linked List Cycle 判断链表是否有环。通过快慢节点可以简单实现。 Unique Binary Search Trees 本题参考了 里面的*How Many Binary Trees Are There?*章节。 The first few terms: C(0) = 1 C(1) = C(0)C(0) = 1 C...

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    linked-list-cycle-ii 链表 linked-list-cycle 链表 copy-list-with-random-pointer 复杂度 single-number 动态规划 candy 贪心 gas-station 动态规划 palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to...

Global site tag (gtag.js) - Google Analytics