Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-29 Thread Navin Naidu
@Umer: Its a singly LL and it has cycle. Both N1 and N2 will keep traversing within the cycle. On Sun, Mar 28, 2010 at 9:56 PM, Umer Farooq the.um...@gmail.com wrote: I'll appreciate comments on the solution proposed by me. It works the following way: take two pointers, N1 and N2 pointing

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-29 Thread Umer Farooq
that's why i have a terminating condition. It will keep on iterating until ((N2 != head)(N2-NextPtr != head)) is true. head pointer of the linked list will be passed as an argument to the function. On Mon, Mar 29, 2010 at 7:04 AM, Navin Naidu navinmna...@gmail.com wrote: @Umer: Its a singly LL

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-29 Thread Navin Naidu
that will result into an infinite loop, On Mon, Mar 29, 2010 at 9:51 PM, Umer Farooq the.um...@gmail.com wrote: that's why i have a terminating condition. It will keep on iterating until ((N2 != head)(N2-NextPtr != head)) is true. head pointer of the linked list will be passed as an

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-29 Thread Prashant K
@Umer Farooq but cycle can be between the nodes(not like circular list) so we may not get head node. @all i think mukesh answer is right On Mon, Mar 29, 2010 at 9:21 AM, Umer Farooq the.um...@gmail.com wrote: that's why i have a terminating condition. It will keep on iterating until ((N2 !=

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-28 Thread vikrant singh
@black diamond i think u may b wrong. check out this 1-2-3-4-5-6-7-8-9-3-. . . . . . On Sat, Mar 27, 2010 at 11:04 AM, blackDiamond patidarc...@gmail.comwrote: take Two pointers increase one by 1 and other by two node if they match somewhere Firstone will become the middle.. On Sat, Mar 27,

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-28 Thread vikrant singh
@mukunda , looks like a v nice solution , but can u xplain the step 2 :line2 a bit more clearly? what do u mean by highest length pointer ? Please give an example to your solution. On Sun, Mar 28, 2010 at 9:23 AM, mukunda n s mukundn...@gmail.com wrote: Simplification of the problem would be

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-28 Thread Rohit Saraf
sorry, i forgot to see *singly* linked list. what about doing a topological sort and returning the middle element. -Rohit On Sun, Mar 28, 2010 at 11:54 AM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: @sanjana: but what in case of 1-2-3-1-4-5-6 -Rohit On Sat, Mar 27, 2010 at 11:19

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-28 Thread mukunda n s
this is impossible case.. how can node1 point to node2 as well as node4?... On Sun, Mar 28, 2010 at 11:54 AM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: @sanjana: but what in case of 1-2-3-1-4-5-6 -Rohit On Sat, Mar 27, 2010 at 11:19 PM, Sanjana - sanjana.2...@gmail.comwrote: For

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-28 Thread saurabh gupta
@Rohit we have a cycle here using hare/tortoise find a node in the cycle find the length of the cycle. find the 'end' of the list, call it E Now equivalent to a singly linked list with the modified termination condition (you need to skip E when you hit it for the first time though) OR find the

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-28 Thread saurabh gupta
@Rohit we have a cycle here On Sun, Mar 28, 2010 at 2:42 PM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: that is topologcall sort On 3/28/10, saurabh gupta sgup...@gmail.com wrote: @Rohit we have a cycle here using hare/tortoise find a node in the cycle find the length of the cycle.

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-28 Thread Rohit Saraf
sry... i got confused -Rohit On Sun, Mar 28, 2010 at 3:14 PM, saurabh gupta sgup...@gmail.com wrote: @Rohit we have a cycle here On Sun, Mar 28, 2010 at 2:42 PM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: that is topologcall sort On 3/28/10, saurabh gupta sgup...@gmail.com wrote:

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-28 Thread Rohit Saraf
anyways.. the solution becomes still more trivial... as there can be only one cycle remove it easily and that helps to find the centre. -Rohit On Sun, Mar 28, 2010 at 4:12 PM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: sry... i got confused -Rohit On Sun, Mar 28, 2010 at 3:14

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-27 Thread Ankit Singh
Hi, This one is rather easy.Same approach can be taken here what applies to non circular linked list. 1. Take a single pointer and a counter. Increment the counter till the next of pointer points to head of circular linked list. Reset the temp pointer to head and increment it to counter/2 +

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-27 Thread vivek bijlwan
keep a ptr ( pt1) at the beginning of the list. do increase another pointer ( pt2) by 1 and another pointer(pt3) by 2 while ( pt3 does not point to the same location as pt1) On Sat, Mar 27, 2010 at 11:01 AM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: how do u define middle when there is a

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-27 Thread saurabh gupta
Perhaps, if n is the length (from the beginning to the point where the loop ends after a traversal of the loop) then n/2 is the middle, i.e. length of list / 2 in which case middle is easy to find. Is that the definition, Sanjana ? On Sat, Mar 27, 2010 at 11:01 AM, Rohit Saraf

Re: [algogeeks] Finding the middle of a singly linked list which has a cycle in it

2010-03-27 Thread Sanjana -
For ex if there is 1-2-3-4-5-6-7-8-5 then no. of unique nodes is 8 then the loop keeps on repeating. So the middle is 4 or 5 On Sat, Mar 27, 2010 at 11:01 AM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: how do u define middle when there is a cycle in the list ? -Rohit On Sat, Mar 27,