-
In the above code, I have two linked lists and I want to add the second linked list to the end of my first linked list. However, the Next and Previous pointers of nodes aren't modifiable in the linked list template. Is there a way I can do this without having to iterate through the linked list and add each element individually. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's not possible to have an O(1) insert of one LinkedList into another without side-effects. E.g. if you add list1 into list2 without iterating, then if you remove an element from list2 it will be, effectively, removed from list1 as well. Linked List is not a complicated data structure, if you need a fast merge (and are fine with the side-effects) you can roll your own fairly quickly. |
Beta Was this translation helpful? Give feedback.
It's not possible to have an O(1) insert of one LinkedList into another without side-effects. E.g. if you add list1 into list2 without iterating, then if you remove an element from list2 it will be, effectively, removed from list1 as well.
Linked List is not a complicated data structure, if you need a fast merge (and are fine with the side-effects) you can roll your own fairly quickly.