I'm trying to find a solution in C# to extending a list in O(1)
.
List's AddRange()
method is of course an O(n)
operation.
This should have been something that LinkedList supports, but LinkedList doesn't have a method like AddRangeLast()
, and trying to combine LinkedLists like this:
LinkedList<int> l1 = new LinkedList<int>(new[] { 1, 2, 3 });
LinkedList<int> l2 = new LinkedList<int>(new[] { 11, 12, 13 });
l1.AddLast(l1.First);
Throws this exception:
System.InvalidOperationException: 'The LinkedList node already belongs to a LinkedList.'
Does anyone know of a way to add a list to a list in O(1)
without implementing LinkedList and LinkedListNode myself?
O(1)
would mean changing BOTH lists. See here for more details. – Lifelong