Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenatee two Lists in c++ in O(1) Complexity

We can concatenate two Linked List in O(1) time if we know the last element. So, Is there any way to concatenate two List in C++ using built-in data structures or I have to implement linked list myself then use it?

like image 357
Muidul Alam 2hin Avatar asked Oct 29 '25 19:10

Muidul Alam 2hin


1 Answers

std::list<int> l1 = create();
std::list<int> l2 = create();
l1.splice(l1.end(), l2);

Note that this empties l2 and moves its elements to l1.

like image 189
Sebastian Redl Avatar answered Oct 31 '25 10:10

Sebastian Redl