`
ShXin
  • 浏览: 12987 次
  • 性别: Icon_minigender_2
  • 来自: 天津
社区版块
存档分类
最新评论

Find Circle Entry for Linked List

阅读更多

The thinking about this question comes from Question 142 and 287 in LeetCode.

 

We all know that we can use two pointers to decide if there is a circle in a list:

fast: move 2 steps each time

slow: move 1 step each time

If slow meet fast in a certain moment, then this list has a circle.

 

Now I would like to show how we can find the entry of circle. Here is the demonstration:

 

 

 

First we define some variables:

s: when slow meet fast, the steps slow moves (so fast moves 2s steps)

x: the length from head to circle entry (this is what we  want to know)

c: the length of the circle

a: the length from entry points to meet points

we have: 2s = s + n*c (n is a positive integer)

                 s = x + a

so we have : x + a = n*c

                     x = (n-1)*c + c - a

c - a means the length from meet point to circle entry

we can consider the left side of expression as a new pointer who moves x steps from list head.

the right side: the slow moves n-1 circle plus the length from meet point to the entry.

 

So we can get the entry when new pointer meet slow.

 

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

相关推荐

    Linked List 链表 基础

    链表的基本类型包括单向链表(Singly Linked List)和双向链表(Doubly Linked List)。在单向链表中,每个节点只包含一个指向其后继节点的指针;而在双向链表中,除了包含指向后继节点的指针外,每个节点还包含一个...

    linked list

    creat() linked list;print(),output linked list;insert(),input index, before data(index)insert new data,if index >c,insert into the end. delede(),delete data(index),0;change(),input index_i and index_...

    C++ LINKED LIST insertion

    关于algorithm and data structure的一个linked list的C++的code

    陈越、何钦铭-数据结构作业6:Reversing Linked List链表翻转

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→...

    function definitions for the linked list data structure

    LinkedList.cpp -- function definitions for the linked list data structure

    C++ 用linked list写priority queue

    用linked list来写priority queue

    Lazy Linked-List(论文实现方法)

    《Lazy Linked-List:一种高效的内存管理策略》 在计算机科学中,数据结构是构建算法的基础,而链表作为其中的一员,因其灵活的插入和删除操作,在许多应用场景中都有着广泛的应用。本文将深入探讨一种特殊的链表...

    doubly_linked_list.rar

    在给定的"double_linked_list.rar"文件中,包含了使用双向链表进行排序的实现。排序的目标是将链表中的元素按照数值大小进行排列。这里提到了两种排序算法:选择法和冒泡法。 1. **选择法排序**: 选择法排序是一...

    doubly linked list template

    this is a template design for doubly linked list. it is just a reference for one who want to study template programming.

    Real-Time Concurrent Linked List Construction on the GPU

    One example uses per-pixel linked lists for order-independent transparency; as a consequence, we are able to directly implement fully programmable blending, which frees developers from the ...

    linux 下 operating system chapter2 projet 的代码及编译方法

    In the module entry point, create a linked list containing five struct birthday elements. Traverse the linked list and output its contents to the kernel log buffer. Invoke the dmesg command to ensure ...

    List-CSharp.zip_C# list 顺序_Linked list_c# list声明_c#list类函数_c#lis

    相比之下,单链表(Linked List)是一种线性数据结构,其中每个节点包含数据和指向下一个节点的引用。单链表不支持随机访问,但插入和删除操作通常比数组更快,因为无需移动后续元素。 4. **单链表操作** 在C#中,...

    Lab10_doublylinkedlist_

    "Lab10_doublylinkedlist_" 这个标题暗示我们讨论的是关于链表数据结构的一个实验或练习,具体是双链表(Doubly Linked List)。双链表是一种线性数据结构,每个节点不仅包含数据,还包含两个指针,分别指向其前一个...

    infix to postfix using linked list

    infix to postfix using linked list

    linked list 链表详解 源程序

    在linux环境下已测试,放心使用,这个是根据stanford cs library的linked list文章里的代码敲出来的

    前端大厂最新面试题-linked-list.docx

    "前端大厂最新面试题-linked-list.docx" 本文档主要涵盖了链表(Linked List)相关的知识点,旨在帮助读者更好地理解链表的实现、操作和应用。 1. 环形链表(Ring Linked List) 环形链表是一种特殊的链表结构,...

    LeetCode--Linked List.cpp

    文件中包含了LeetCode中Tag为LinkedList的题目参考代码。

    使用linked list實現字串排序

    本範例採用linked list方式,去讀取一個文字檔案,根據空白字元把文字分成別類,並統計每一個單字出現的次數!

    Leetcode 234. Palindrome Linked List

    方法1  思路:将链表中的元素都保存list中,并判断这个list和反转后的list,是否相同,如果相同,则回文;否则,则不回文。...# Definition for singly-linked list. # class ListNode(object): # def __i

    java-leetcode题解之Linked List Components.java

    java java_leetcode题解之Linked List Components.java

Global site tag (gtag.js) - Google Analytics