0

We know that lookup on a singly linked list is O(n) given a head pointer. Let us say I maintain a pointer at half the linked list at all times. Would I be improving any lookup times?

4 Answers 4

1

Yes, it can reduce the complexity by a constant factor of 2, provided you have some way of determining whether to start from the beginning or middle of the list (typically, but not necessarily, the list being sorted). This is, however, a constant factor, so in terms of big-O complexity, it's irrelevant.

To be relevant to big-O complexity, you need more than a constant factor change. If, for example, you had a pointer to bisect each half, and again each half of that, and so on, you'd end up with logarithmic complexity instead of linear -- and you'd have transformed your "linked list" into an (already well known) threaded tree.

Sign up to request clarification or add additional context in comments.

Comments

0

Nice thought, but this still does not improve the search operation. No matter how many pointers you have at different portions of the list, you still have to analyze each element in the list. However, you -could- two threads to search each half of the list making the operation twice as fast in theory.

2 Comments

Adding threads might improve the running time on certain CPUs, but it doesn't change the algorithm's complexity, which remains O(N).
so our comparisons remain effectively the same even if we've reduced howmany times we loop over the list
0

Only if your linked list's data is sorted. Otherwise, as already said in the other reply.

2 Comments

are you implying a binary search? Aren't binary searches on lists ineffective?
Not exactly a binary search. But you can simply reduce the problem in half if your data is sorted (considering that you have the middle pointer already)
0

It would, but asymptotically it would be still the same. However, there is a data structure that uses this idea, it is called skip list. Skip list is a linked list where some nodes have more pointers that are pointing in some sense to the middle of the rest of list. The idea is well illustrated on this image. This structure usually has logarithmic insert find and delete.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.