How to determine whether there are circles in a singly linked list?
2011-01-10 14:51
513 查看
How to determine whether there are circles in a singly linked list?
Answer:let to point p1 ,and p1 , p1 travels the list with one step length ,and p2 with two step length ,
After t step , if p2 come to same pos with p1 , we get there is an circle , otherwise no circle exist in list.
Now let’s prove it .
Both p1 and p2 travel the list ,but with diff step length, where p1 is a one step traveler and P2 is a two step traveler.
Exist and only exist two conditions:
1. Condition one: P2 come to the end. we get there is on circle , it’s trival.
2. Condition two:P2 come to same pos with p1, we got more than one circle exist.
Let there is a circle as the picture below:
Suppose: When p1 come to the first pos (N1) of the circle, P2 is at pos Nk(where K should be one of 1…N).
After t step :
P1 come to N(t1) where t1=t%N
P2 come to N(t2) whereK2=(k+2t)%N
Principal :
t , t is a natural number , let t+n*m=k+2t , so t1=t2 and P1 come to same pos with P2. where m is natural number
Proof:
Let t=m*n-k we get it, m is a positive natural number.
口.
Answer:let to point p1 ,and p1 , p1 travels the list with one step length ,and p2 with two step length ,
After t step , if p2 come to same pos with p1 , we get there is an circle , otherwise no circle exist in list.
Now let’s prove it .
Both p1 and p2 travel the list ,but with diff step length, where p1 is a one step traveler and P2 is a two step traveler.
Exist and only exist two conditions:
1. Condition one: P2 come to the end. we get there is on circle , it’s trival.
2. Condition two:P2 come to same pos with p1, we got more than one circle exist.
Let there is a circle as the picture below:
Suppose: When p1 come to the first pos (N1) of the circle, P2 is at pos Nk(where K should be one of 1…N).
After t step :
P1 come to N(t1) where t1=t%N
P2 come to N(t2) whereK2=(k+2t)%N
Principal :
t , t is a natural number , let t+n*m=k+2t , so t1=t2 and P1 come to same pos with P2. where m is natural number
Proof:
Let t=m*n-k we get it, m is a positive natural number.
口.
相关文章推荐
- Convert Sorted List to Binary Search Tree -- leetcodeGiven a singly linked list where elements are s
- Problem 15 - Starting in the top left corner in a 20 by 20 grid, how many routes are there to the bottom right corner?
- How to determine which checkboxes are checked in postback (sqlstr="delete spprod where id in ("&request("sid")&")")
- 如何反转一个单链表 How to reverse a singly linked list
- How to solve problem caused when the CRT library and MFC libraries are linked in the wrong order
- How to pass List from Controller to View in MVC 3.
- How to get position of key/value in LinkedHashMap using its key
- How to Check Whether Oracle Binary/Instance is RAC Enabled and Relink Oracle Binary in RAC (文档 ID 28
- [转]How to tell whether your CPU is running in real mode or protected mode
- How to clear the views which are held in the ListView's RecycleBin?
- how to copy a linkedlist
- How to send a list of files over a socket in Java
- 【10】Find nth to last element of a singly linked list
- [转]How to Use xp_dirtree to List All Files in a Folder
- How to implement check box in listView Android to delete listItems
- 【原】How to fix: Handler “PageHandlerFactory-Integrated” has a bad module “ManagedPipelineHandler” in its module list
- How To Fix “Problem with MergeList /var/lib/apt/lists” Error In Ubuntu 11.04
- [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点
- How to delete a large number of data in SharePoint for List when refreshing data?
- How to check in Windows if you are using UEFI