3

Let's say I have the following:

LinkedList<int> list1 = new LinkedList<int>();
LinkedList<int> list2 = new LinkedList<int>();

list1.AddLast(1);
list1.AddLast(2);

list2.AddLast(1);
list2.AddLast(2);

As far as I know you cannot do the following;

list1.AddLast(list2.First);

and except the lists to be connected together.

What is the proper way to merge two LinkedLists in C#? I know there is a Union() method, but it seems like such strong point of LinkedList in C++ is that you can easily combine and break lists apart if need be.

The LinkedList class does not support chaining, splitting, cycles, or other features that can leave the list in an inconsistent state.

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
  • check this question - http://stackoverflow.com/questions/1094445/how-does-one-add-a-linkedlistt-to-a-linkedlistt-in-c – Dmitry Khryukin Nov 16 '12 at 01:50
  • 1
    Try to use lists instead. see here why: http://stackoverflow.com/questions/169973/when-should-i-use-a-list-vs-a-linkedlist – RAS Nov 16 '12 at 02:01

1 Answers1

4

It's equally simple if you use a List instead of a LinkedList. Here are a couple of ways to do the whole list.

LINQ;

var combinedList = list1.Concat(list2).ToList();

Other way I found on msdn;

List<int> combinedList = new List<int>();
combinedList.AddRange(list1);
combinedList.AddRange(list2);
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • 2
    I picked the LinkedList because I figured for combining and splitting lists it would be the ideal choice. Are Lists all around better for this type of problem? – afuzzyllama Nov 16 '12 at 01:48
  • I would say so. Particularly because LINQ has a `ToList()` and I don't think it has a `ToLinkedList()` equivalent. According to this post http://stackoverflow.com/questions/5983059/why-is-a-linkedlist-generally-slower-than-a-list linked lists also have worse performance. – evanmcdonnal Nov 16 '12 at 01:50
  • 1
    In my personal testing, I was unable to come up with a case where a LinkedList worked better than a standard .NET List. I tested insertion, concatenation, deletion, iteration, etc. There was never a case where a linked list was definitively better than the standard List class. – cwharris Nov 16 '12 at 01:58
  • @evanmcdonnal, LinkedLists have better performance when adding, removing records. especially, you can see the performance improvement when removing items from middle of the list. – RAS Nov 16 '12 at 02:00
  • @user1792936 I highly doubt that. List is an abstraction built with an array of pointers at its core. The only time performance would be worse is when you're growing so much that you have to allocate a new array. If I want to remove something from the middle of a LinkedList I have to walk all the links, delete the object, re assign 4 pointers then delete that node. If I want to remove it from the array I just loop over it, delete the object and set that index to NULL. – evanmcdonnal Nov 16 '12 at 02:36
  • 2
    Microsoft documentation says RemoveAt on a list is "an O(n) operation, where n is (Count - index)", which implies it has to shift contents up. Thus, removing from beginning or end should be faster with linkedlist (RemoveFirst/RemoveLast) – Garr Godfrey Oct 02 '17 at 18:39
  • Although, RemoveAt(list.Count-1) would be O(1), so just as fast as RemoveLast. List just suffers removing the first element. – Garr Godfrey Oct 02 '17 at 18:41