title: To merge two sorted linked lists. Is there a better way to create new Linked list heads in java?
body: <p>In this program, I have created the head node inside the class LinkedList.</p>
  <pre><code>public class LinkedList {
  public class Node {
      public int data;
      public Node next;
  }
  
  Node Head=null
  void IntersectionOfTwoSortedLists(Node ListA, Node ListB){
  
      while (ListA!=null &amp;&amp; ListB!=null) {
          if (ListA.data == ListB.data){
              System.out.println(ListA.data);
              InsertAtEnd(ListA.data);
          }
          if(ListA.data&lt;ListB.data)
              ListA=ListA.next;
          else
              ListB=ListB.next;
      }
  }
  </code></pre>
  <p>and the main function in a different class looks like this.</p>
  <pre><code>public static void main(String[] args) {
      LinkedList list = new LinkedList();
      LinkedList list2= new LinkedList();
      for(int i=0;i&lt;n;i++){
          list.InsertAtEnd(sc.nextInt());
      }
      System.out.println(&quot;Enter the list 2 elements&quot;);
      for(int i=0;i&lt;n;i++){
        list2.InsertAtEnd(sc.nextInt());
      }
  
      LinkedList intersect= new LinkedList();
      intersect.IntersectionOfTwoSortedLists(list.Head,list2.Head);
  </code></pre>
  <p>creating a new object of the linked list class every time a good practice or is there a better way to do this? Thanks in advance!</p>