Re: Swap nodes in a linked list
Fabian Schmied <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.clr |
|---|---|
| Message-ID | <[email protected]> |
> However having the index, doesn't > that mean that you can simply move to the previous node using that index, and > then place the removed node in front of that? (so first determine previous > node (ElementAt(n-1)) then, addBefore(previousNode) If I understand it correctly, Enumerable.ElementAt in conjunction with a LinkedList will not give you the LinkedListNode, but the actual element of the list. Therefore, you can't easily get to the previous node from an index using Enumerable.ElementAt. Using LinkedList.Find to get the node with the element retrieved via Enumerable.ElementAt is quite inefficient, because you enumerate the list twice; also, it won't work correctly if the list contains duplicate values. Therefore, I'd suggest looping over the list _nodes_ (not its elements) yourself until the node at the specified position is found, then you automatically have all the possibilities. (It's not as short, but efficient. There doesn't seem to be a simple way to get an enumerator for the nodes in a LinkedList, or at least I can't see one.) Fabian =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com