[ACCEPTED]-How does one add a LinkedList<T> to a LinkedList<T> in C#?-linked-list
Yes, you have to loop, unfortunately. This 23 is an O(n) operation - O(1) for each entry 22 added. There's no risk of requiring a buffer 21 to be resized and copied, etc - although 20 of course garbage collection might do roughly 19 that :) You could even write handy extension 18 methods:
public static class LinkedListExtensions
{
public static void AppendRange<T>(this LinkedList<T> source,
IEnumerable<T> items)
{
foreach (T item in items)
{
source.AddLast(item);
}
}
public static void PrependRange<T>(this LinkedList<T> source,
IEnumerable<T> items)
{
LinkedListNode<T> first = source.First;
// If the list is empty, we can just append everything.
if (first is null)
{
AppendRange(source, items);
return;
}
// Otherwise, add each item in turn just before the original first item
foreach (T item in items)
{
source.AddBefore(first, item);
}
}
}
EDIT: Erich's comment suggests why 17 you might think this is inefficient - why 16 not just join the two lists together by 15 updating the "next" pointer of 14 the tail of the first list and the "prev" pointer 13 of the head of the second? Well, think about 12 what would happen to the second list... it would 11 have changed as well.
Not only that, but 10 what would happen to the ownership of those 9 nodes? Each is essentially part of two lists 8 now... but the LinkedListNode<T>.List
property can only talk about 7 one of them.
While I can see why you might 6 want to do this in some cases, the way that 5 the .NET LinkedList<T>
type has been built basically 4 prohibits it. I think this doc comment explains 3 it best:
The
LinkedList<T>)
class does not support chaining, splitting, cycles, or 2 other features that can leave the list in 1 an inconsistent state.
llist1 = new LinkedList<T>(llist1.Concat(llist2));
this concatenates the two lists (requires 4 .NET 3.5). The drawback is that it creates 3 a new instance of LinkedList, which may 2 not be what you want... You could do something 1 like that instead :
foreach(var item in llist2)
{
llist1.AddLast(item);
}
Here you can find my linked list implementation 11 with O(1) concat and split times.
Why .NET LinkedList does not support Concat and Split operations?
Short summary
Advantages 10 vs .NET LinkedList:
Less memory consumption, thus 9 every node SimpleLinkedListNode has three 8 pointers (prev, next, value) instead of 7 four (prev, next, list, value) unlike original 6 .NET implementation.
Supports Concat and 5 Split operations in O(1)
Supports IEnumarable 4 Reverse() enumerator in O(1) – by the way 3 I do not see any reason why it’s not provided natively 2 on the .NET LinkedList. Appropriate extension 1 method requires O(n).
Disadvantages:
- Does not support Count.
- Concat operation leaves second list in an inconsistent state.
- Split operation leaves original list in an inconsistent state.
- You are able to share nodes between lists.
Other:
- I have chosen an alternative strategy for implementing enumeration and find operations, rather than more verbose and purely readable original implementation. I hope the negative performance impact remains insignificant.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.