How do I get the n-th element in a LinkedList?

前端 未结 4 984
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 14:26

How can I get the n-th element of a LinkedList instance? Is there a built-in way or I might need to introduce my own implementation? For example an extension method?

4条回答
  •  一个人的身影
    2020-12-11 15:06

    You can do it with LINQ as in list.ElementAt(n) or list.Skip(n - 1).First() , but if you find yourself making indexed access into a linked list you are probably doing something wrong (linked lists do not efficiently support this operation). Perhaps another data structure would be more appropriate?

提交回复
热议问题