How to add the first element of a list to another list in C++

1 Answer

0 votes
#include <iostream>
#include <list>

int main()
{
    // initializing lists 
    std::list<int> l1 = { 1, 2 }; 
    std::list<int> l2 = { 3, 4, 5 }; 

    std::list<int>::iterator it2; 
    it2 = l2.begin();
    
    // transfer the first element of l2 to l1
    l1.splice(l1.end(), l2, it2);
    
    for (auto i : l1) {
        std::cout << i << " "; 
    }
}



/*
run:

1 2 3 

*/

 



answered Oct 15, 2025 by avibootz
...