C++ program to swap links of a Linked List In this code I tried to swap links in a linked list instead of swapping data.Please Review my code.

#include &lt;iostream&gt;

    template&lt;class T&gt;
    class LinkedList
    {
struct Node{
  T data;
  Node * next;
  Node(T val): data(val), next(nullptr){}
};

Node* head;

public:

  LinkedList() : head(nullptr) {}

  void insert(T val)
  {
    Node* node = new Node(val);
    Node* tmp  = head;

    if(tmp == nullptr)
    {
      head = node;
    }
    else
    {
      while(tmp-&gt;next != nullptr)
      {
        tmp = tmp-&gt;next;
      }
      tmp-&gt;next = node;
    }
  }

  void swapLinks(T val1, T val2)
  {
    Node* prevNode1 = nullptr;
    Node* prevNode2 = nullptr;
    Node* nextNode1 = nullptr;
    Node* nextNode2 = nullptr;
    Node* node      = head;
    Node* node1     = search(val1);
    Node* node2     = search(val2);

    while(node-&gt;next != node1)
    {
      node = node-&gt;next;
    }
    if(node-&gt;next == node1)
    {
      prevNode1 = node;
      nextNode1 = node1-&gt;next;
    }

    node = head;

    while(node-&gt; next != node2)
    {
      node = node-&gt;next;
    }
    if(node-&gt;next == node2)
    {
      prevNode2 = node;
      nextNode2 = node2-&gt;next;
    }

    prevNode1-&gt;next = node2;
    Node* tmp       = node2-&gt;next;
    node2-&gt;next     = node1-&gt;next;
    prevNode2-&gt;next = node1;
    node1-&gt;next     = tmp;
  }

  friend std::ostream &amp; operator &lt;&lt;(std::ostream &amp; os, LinkedList&lt;T&gt;&amp; ll)
  {
    ll.display(os);
    return os;
  }

private:

  struct Node *search(T val)
  {
    Node* node = head;
    while(node != nullptr)
    {
      if(node-&gt;data == val)
      {
        return node;
      }
      node = node-&gt;next;
    }
    std::cerr &lt;&lt; "No such element in the List \n";
    return nullptr;
  }

  void display(std::ostream&amp; out = std::cout) const
  {
    Node* node = head;
    while(node != nullptr)
    {
      out &lt;&lt; node-&gt;data &lt;&lt;" ";
      node = node-&gt;next;
    }
  }
};

int main()
{
  LinkedList&lt;int&gt; ll1;
  ll1.insert(10);
  ll1.insert(15);
  ll1.insert(12);
  ll1.insert(13);
  ll1.insert(28);
  ll1.insert(14);
  ll1.insert(16);
  std::cout &lt;&lt; "LinkedList1 : "&lt;&lt; ll1 &lt;&lt;"\n";
  ll1.swapLinks(12, 14);
  std::cout &lt;&lt; ll1 &lt;&lt;"\n";


}


Is there any another solution or better solution? Please suggest.
This works as long as 2 conditions are met.


val1 and val2 are in the list.
Neither are in the head node.


Otherwise you access stuff via null pointers:

  void swapLinks(T val1, T val2)
  {
    Node* prevNode1 = nullptr;
    Node* prevNode2 = nullptr;
    Node* nextNode1 = nullptr;
    Node* nextNode2 = nullptr;
    Node* node      = head;
    Node* node1     = search(val1);
    Node* node2     = search(val2);

    // This will not find values in the "head node"
    // If the value is in head the you will not find a previous node.
    // You need to take into account this special case at the bottom.
    while(node-&gt;next != node1)
    {
      node = node-&gt;next;
    }
    if(node-&gt;next == node1)
    {
      prevNode1 = node;
      nextNode1 = node1-&gt;next;
    }

    node = head;

    // This will not find values in the "head node"
    // If the value is in head the you will not find a previous node.
    // You need to take into account this special case at the bottom.
    while(node-&gt; next != node2)
    {
      node = node-&gt;next;
    }
    if(node-&gt;next == node2)
    {
      prevNode2 = node;
      nextNode2 = node2-&gt;next;
    }

    // HERE YOU HAVE some issues.
    // prevNode1 or prevNode2 or node1 or node2 may be nullptr
    // If any of these values are null the code below will be UB.
    prevNode1-&gt;next = node2;
    Node* tmp       = node2-&gt;next;
    node2-&gt;next     = node1-&gt;next;
    prevNode2-&gt;next = node1;
    node1-&gt;next     = tmp;
  }