Middle of a singly linked list of unknown length

  • Thread starter Himanshu Chauhan
  • Start date
H

Himanshu Chauhan

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
 
D

Dan

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.
 
R

Richard Tobin

Himanshu Chauhan said:
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
added to the end.

-- Richard
 
R

rahul

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.
 
J

Jens Thoms Toerring

Himanshu Chauhan said:
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
 
K

Kenneth Brody

Himanshu said:
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: <mailto:[email protected]>
 
B

Bartc

Kenneth Brody said:
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.
 
K

Keith Thompson

CBFalconer said:
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.
 
A

Antoninus Twink

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.)
 
R

Richard Tobin

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
 
K

Kenneth Brody

Bartc said:
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.

Perhaps I should have included my implied: "I'll tell you when you
get to the end." (Just as a singly-linked list has a "destination
not yet revealed" until you reach the end.)
Better, 'stop halfway to the end of the road of unknown length'.

Same thing.
Easily done by traversing the entire length one and a half times.

Which doesn't qualify as "first parse['pass'?]" as stated by the OP.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
A

Antoninus Twink

I think those so-called solutions are ruled out by the O.P.'s
requirement to get the answer "in the first parse" over the list,
which I read as a garbled form of "in the first pass" over the list.
Using two pointers means using one-and-a-half passes.
[snip]

I interpreted the OP's requirement as a garbled form of an old chestnut
textbook exercise.
Chuck, I hereby chastise you for answering an off-topic question
instead of sending the questioner to comp.programming where he belongs
and where he'll get better answers.

Even the people in comp.programming aren't able to do the impossible.
 
K

Keith Thompson

CBFalconer said:
To have the single list, you have to form it, by adding one item at
a time. The break into two portions does this. If you don't like
having two lists during formation, just have one, and add items
alternately before the 'middle' and at the 'end'.

I still say you're changing the requirements.

You can also keep track of the number of items in the list as you
create it, or you can store the nodes of the linked list in a
contiguous array, either of which makes it possible to find the middle
without traversing the whole thing.

But the problem as stated referred to "a singly linked list of unknown
length", presumably one given to you by some outside source.
 
U

user923005

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?

No. The obvious solution is to add a length element to the head of
your list.

I don't think I have ever used linked lists without list lengths.

I want to know how many things are in my list all the time (though I
rarely will want to know if something is the middle item or not).

Here is a list with a counter:
http://mij.oltrelinux.com/devel/simclist/

I'm a lot more likely to use a skiplist than a singly linked list:
http://sourceforge.net/projects/skiplist/

Memory cost is similar and they are better if you ever have to find
something in it. Most of the time, you will.
 
R

Richard Bos

CBFalconer said:
To have the single list, you have to form it,

Not true; someone else may have formed it for you.

And that's just the _trivial_ objection to your solution.

Richard
 
K

Kevin D. Quitt

Humm... I think I just did somebody's homework!

Probably. I didn't look at your code, but it looks more complex than what
I was thinking: Two pointers start at the head. Each time you move one of
the two, more the other one just one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top