Tuesday 15 April 2014

c# - Most efficent method for getting 10th item from a singlurary linked list -



c# - Most efficent method for getting 10th item from a singlurary linked list -

what efficient method getting 10th item form end of list

i thinking like:

list[list.count() - 10];

if you're using list<t> system.collections.generic you're not using singly linked list. it's backed array, , can simple access index, suggested:

list[list.count - 10];

it o(1) operation. should check if list has @ to the lowest degree 10 elements before doing don't exception.

however, if have own singly linked list construction you'll have iterate entire list nth item end of list. can utilize same approach, forcefulness 2 round trips on collection - first total number of elements , sec nth lastly element.

you can create happen 1 iteration, if store lastly n items you've seen, e.g. in queue. o(n) operation.

c#

No comments:

Post a Comment