Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mid point in a link list in a single traversal?

I'm trying to find the point of a singly link list where a loop begins. what I thought of was taking 2 pointers *slow, *fast one moving with twice the speed of other. If the list has a loop then at some point

    5-6-7-8
    |     |
1-2-3-4-7-7

slow=fast

Can there be another elegant solution so that the list is traversed only once?

like image 883
ishan soni Avatar asked Aug 10 '12 21:08

ishan soni


People also ask

Can you go to the middle of a Linkedlist with only one traversal?

The question demands to find the middle of a singly linked list. We can simply find the total length of the linked list, in this way we can identify which node falls in the middle. To find the middle node, we can traverse again until we reach (length/2)th node. Create a pointer p , pointing to the head.

How will you find the mid of a Linkedlist in a single iteration?

In each iteration, the ptr1 will access the two nodes and the ptr2 will access the single node of the linked list. Now, when the ptr1 reaches the end of the linked list, the ptr2 will be in the middle. In this way, we are able to get the middle of linked list in a single iteration.

How do you find the middle item in a linked list?

Method 1: Traverse the whole linked list and count the no. of nodes. Now traverse the list again till count/2 and return the node at count/2.


2 Answers

Your idea of using two walkers, one at twice the speed of the other would work, however the more fundamental question this raises is are you picking an appropriate data structure? You should ask yourself if you really need to find the midpoint, and if so, what other structures might be better suited to achieve this in O(1) (constant) time? An array would certainly provide you with much better performance for the midpoint of a collection, but has other operations which are slower. Without knowing the rest of the context I can't make any other suggestion, but I would suggest reviewing your requirements.

like image 153
Andrew Ring Avatar answered Oct 01 '22 01:10

Andrew Ring


I am assuming this was some kind of interview question.

If your list has a loop, then to do it in a single traversal, you will need to mark the nodes as visited as your fast walker goes through the list. When the fast walker encounters NULL or an already visited node, the iteration can end, and your slow walker is at the midpoint.

There are many ways to mark the node as visited, but an external map or set could be used. If you mark the node directly in the node itself, this would necessitate another traversal to clean up the mark.

Edit: So this is not about finding the midpoint, but about loop detection without revisiting already visited nodes. Marking works for that as well. Just traverse the list and mark the nodes. If you hit NULL, no loop. If you hit a visited node, there is a loop. If the mark includes a counter as well, you even know where the loop starts.

like image 41
jxh Avatar answered Oct 01 '22 01:10

jxh