Question Union of two LinkedLists

andrews

Well-known member
Joined
Nov 22, 2011
Messages
134
Programming Experience
5-10
Question about the performance of joining two LinkedLists.
f.e.
LL1 = {10,12,15,17,20,30,....}
LL2 = {37,58,67,59,47,65,....}
I can do :
For each number as integer in LL2
LL1.AddLast(number)
Next
If LL2 contains 1000 numbers then there are 1000 actions
But I was thinking LL2.First refer to the second node and so on to do this in one action.
Is this possible ?
Thanks for any response
 
You should probably use LINQ

If the values re hardcoded then do this
        Dim result1 = From i In {10, 12, 15, 17, 20, 30} Select i
        Dim result2 = From i In {37, 58, 67, 59, 47, 65} Select i
        Dim list = result1.Union(result2).ToList


If you are using 2 variable arrays

        Dim LL1 As Integer() = {10, 12, 15, 17, 20, 30}
        Dim LL2 As Integer() = {37, 58, 67, 59, 47, 65}

        Dim result1 = From i In LL1 Select i
        Dim result2 = From i In LL2 Select i
        Dim list = result1.Union(result2).ToList


Remember to Union 2 queries the schema needs to be identical
 
Sorry, it is concerning two LinkedLists.
And checking the Internet I think that it is not possible with LinkedLists.
 
You can use the CopyTo method to transfer all items of both lists to an array, and constructor to create a new LinkedList from that array. A shortcut to that is actually very close to previous reply, Linq extensions like Union and Concat works fine with these lists:
Dim ll3 = New LinkedList(Of Integer)(LL1.Concat(LL2))
 
The question is not if it is possible (see my example) but to do it in a performanve way.
I think now for trying to make a single list class to combine two single lists by connecting just two nodes.
 
That is not possible, a LinkedListNode can only belong to one list and its links can only refer to other nodes in same list. LinkedList maintains link intergrity no matter how you add items to it, and you can't change the node links.
 
Back
Top