Velocity Reviews > Middle of a singly linked list of unknown length

# Middle of a singly linked list of unknown length

Himanshu Chauhan
Guest
Posts: n/a

 06-06-2008
Hi!

I was wondering, In the first parse of a singly linked list of unknown
length, is it possible to know when we are at middle of the linked list?

Regards
--Himanshu

Dan
Guest
Posts: n/a

 06-06-2008

> I was wondering, In the first parse of a singly linked list of unknown
> length, is it possible to know when we are at middle of the linked list?

You could do it if you kept track of how many were in the list when you
built it.

Richard Tobin
Guest
Posts: n/a

 06-06-2008
In article <g2atn8\$k9f\$(E-Mail Removed)>,
Himanshu Chauhan <(E-Mail Removed)> wrote:

>I was wondering, In the first parse of a singly linked list of unknown
>length, is it possible to know when we are at middle of the linked list?

Obviously not, since nothing would be different if extra elements were

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

rahul
Guest
Posts: n/a

 06-06-2008
On Jun 6, 1:48 pm, Himanshu Chauhan <(E-Mail Removed)> wrote:
> Hi!
>
> I was wondering, In the first parse of a singly linked list of unknown
> length, is it possible to know when we are at middle of the linked list?
>
> Regards
> --Himanshu

You can keep the count of nodes inserted as a static or global
variable. By counting
the number of iterations, you can decide if you are in the middle of
the list.

Just out of curiosity, this question is meant for some real code or is
it just like that.

Jens Thoms Toerring
Guest
Posts: n/a

 06-06-2008
Himanshu Chauhan <(E-Mail Removed)> wrote:
> Hi!

> I was wondering, In the first parse of a singly linked list of unknown
> length, is it possible to know when we are at middle of the linked list?

Are you looking for the old trick of using two pointers, the first
one always getting set to the next element, the other one being set
to the successor of the next element so, when the second pointer
reaches the end of the list, the first one points to the middle
element? But I don't know if that is allowed by what you call
"first parse".

BTW, this hasn't anything to do with C, so such a question is
much better suited for comp.programming.

Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de

Kenneth Brody
Guest
Posts: n/a

 06-06-2008
Himanshu Chauhan wrote:
>
> Hi!
>
> I was wondering, In the first parse of a singly linked list of unknown
> length, is it possible to know when we are at middle of the linked list?

The me rephrase the problem, and see if you can find a solution:

Drive down this road and stop halfway to a destination which I
have not yet revealed.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

Bartc
Guest
Posts: n/a

 06-06-2008

"Kenneth Brody" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Himanshu Chauhan wrote:
>>
>> Hi!
>>
>> I was wondering, In the first parse of a singly linked list of unknown
>> length, is it possible to know when we are at middle of the linked list?

>
> The me rephrase the problem, and see if you can find a solution:
>
> Drive down this road and stop halfway to a destination which I
> have not yet revealed.

That's not quite the same. In that case you would not know when you got to
the destination so it's unsolveable. A linked list however has a definite
end point, assuming it's not circular.

Better, 'stop halfway to the end of the road of unknown length'. Easily done
by traversing the entire length one and a half times.
--
Bartc

Keith Thompson
Guest
Posts: n/a

 06-06-2008
CBFalconer <(E-Mail Removed)> writes:
> Himanshu Chauhan wrote:
>> I was wondering, In the first parse of a singly linked list of
>> unknown length, is it possible to know when we are at middle of

>
> Yes. Just build the list in two halves, and join them when
> building is done. The head of the second half will be the
> midpoint, withing an error of one.

Right, finding a solution is easy if you're allowed to redefine the
problem.

The problem statement assumed "a singly linked list of unknown
length", not two lists of equal length.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Antoninus Twink
Guest
Posts: n/a

 06-06-2008
On 6 Jun 2008 at 16:36, Keith Thompson wrote:
> CBFalconer <(E-Mail Removed)> writes:

[snip nonsense]
>
> Right, finding a solution is easy if you're allowed to redefine the
> problem.

Vintage CBF. At least two people have already provided a correct
solution (i.e. run two pointers through the list, one travelling at
half the speed of the other), but several hours later in wades Chuck
with something completely wrong-headed. (Still, I suppose at least he
tried, albeit unsuccessfully, to answer the damn question for once,
instead of telling the OP to get lost and try comp.programming.)

Richard Tobin
Guest
Posts: n/a

 06-06-2008
In article <(E-Mail Removed)>,
Antoninus Twink <(E-Mail Removed)> wrote:
>At least two people have already provided a correct
>solution (i.e. run two pointers through the list, one travelling at
>half the speed of the other)

That's not a solution to the problem as I interpreted it. It uses
one-and-a-half passes over the list, rather than stopping in the
middle during the first pass.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)