Merge two sorted Linked List I want to improve this code. How to enter all elements in linked list simultaneously? I have not written code to delete a node becauseI want to only merge list for learning purpose.

#include &lt;iostream&gt;
#include &lt;utility&gt;

template&lt;class T&gt;
class LinkedList
{
  struct Node
  {
    T data;
    Node * next = nullptr;
    Node()          : data(), next(nullptr) {}
    Node(T&amp;&amp; value) : data(std::move(value)), next(nullptr) {}
  };

  Node *head;
public:
  LinkedList() : head(nullptr) {}
  LinkedList(const LinkedList&amp; ll) = delete; //copy constructor
  LinkedList&amp; operator=(const LinkedList&amp; ll) = delete; //copy assignment
  ~LinkedList()
  {
    Node *tmp = nullptr;
    while(head)
    {
      tmp = head;
      head = head-&gt;next;
      delete tmp;
    }
    head = nullptr;
  }

   void insert(T&amp;&amp; value)
   {
    Node *node = new Node(std::move(value));
    Node *tmp = head;
    if(tmp == nullptr)
    {
      head = node;
    }
    else
    {
      while(tmp-&gt;next != nullptr)
      {
        tmp = tmp-&gt;next;
      }
      tmp-&gt;next = node;
    }
  }

  void mergeSortedList(LinkedList&lt;T&gt;&amp; ll2)
  {
    Node *node = new Node();
    node-&gt;next = nullptr;
    Node *tmp = nullptr;
    tmp = node;

    Node *head1 = head;
    Node *head2 = ll2.head;

    while(head1 != nullptr &amp;&amp; head2 != nullptr)
    {
      if(head1-&gt;data &lt;= head2-&gt;data)
      {
        tmp-&gt;next = head1;
        tmp = tmp-&gt;next;
        head1 = head1-&gt;next;
      }
      else
      {
        tmp-&gt;next = head2;
        tmp = tmp-&gt;next;
        head2 = head2-&gt;next;
      }
    }
    while(head1 != nullptr &amp;&amp; head2 == nullptr)
    {
      tmp-&gt;next = head1;
      tmp = tmp-&gt;next;
      head1 = head1-&gt;next;
    }
    while(head2 != nullptr &amp;&amp; head1 == nullptr)
    {
      tmp-&gt;next = head2;
      tmp = tmp-&gt;next;
      head2 = head2-&gt;next;
    }
    tmp = tmp-&gt;next;
    delete tmp;

    // head of the new list
    head = node-&gt;next;
  }

  void printList()
  {
    Node *node = head;
    if(node == nullptr)
    {
      std::cout &lt;&lt; "Empty List \n";
    }
    while(node != nullptr)
    {
      std::cout &lt;&lt; node-&gt;data &lt;&lt; " ";
      node = node-&gt;next;
    }
    std::cout &lt;&lt; "\n";
  }
};

int main()
{
  LinkedList&lt;int&gt; ll1;
  ll1.insert(5);
  ll1.insert(10);
  ll1.insert(18);
  ll1.insert(25);
  std::cout &lt;&lt; "List 1 : ";
  ll1.printList();

  LinkedList&lt;int&gt; ll2;
  ll2.insert(6);
  ll2.insert(8);
  ll2.insert(11);
  ll2.insert(20);
  ll2.insert(23);
  ll2.insert(28);
  ll2.insert(40);
  std::cout &lt;&lt; "List 2 : ";
  ll2.printList();

  ll1.mergeSortedList(ll2);
  std::cout &lt;&lt; "Merged List : ";
  ll1.printList();
}
Node constructor

It seems odd that we can only provide rvalues as argument to the constructor.  I'd expect to be able to pass by copy, too.

The usual way to allow that is to accept a T by value, and move-construct data from that value:

Node(T value = T()) : data(std::move(value)) {}


Note two other changes there: I've added a default argument, so we don't need to write a default constructor, and I've omitted the initialization of next, as we wrote a suitable default initializer for it.

List constructor

If we provide an initializer for head, we don't need to write a default constructor.

We might want to accept a list of elements when creating a linked list.  That would look something like this:

LinkedList() = default;
LinkedList(std::initializer_list&lt;T&gt; elements)
{
    for (auto&amp; e: elements)
         insert(std::move(e));
}


Pointer ownership

We need to be absolutely clear that every object created with new has exactly one owner, that knows when the object is no longer in use and will delete it at that point.  When moving nodes from one list to another, it is essential that the ownership is transferred - if both lists think they are owner of a node, it will be double-deleted, and if neither lists does, it will be leaked.

We can write code to do this manually, but it's easier and less error-prone to #include &lt;memory&gt; and then make use of the smart pointer types that are provided for us.