title: Circular Linked List In Ascending Order Java
body: <p>My task is to implement a circular linked list in java (ascending order) but the problem is that it is going in an infinite loop</p>
  
  <p>I have created a class of Node in which i have define two elements.</p>
  
  <pre><code>public class Node {
      public int element;
      public Node next;
  
      public class Node {
          int element;
          Node next;
      }
  }
  </code></pre>
  
  <p>Now in the second class of List i have made a insert function i have define a Node head=null in the start and create a new nNode .After that i am checking in the head section if head==null then the first element will be nNode. After inserting the first element i will compare the next element and the head element if the head element is greater than it will shift next and the new nNode will be the head. Since it is the circular linked list it is working but it is also going in an infinite loop.</p>
  
  <p>This is the List class in which i have use the node class variables</p>
  
  <pre><code>public class List {
      void insert(int e) {
          Node nNode = new Node();
          Node tNode = head;
          nNode.element = e;
  
          if (head == null)
              head = nNode;                      
          else if (head.element &gt; e) {                        
              nNode.next = head;
              head=nNode;
          } else {
              Node pNode = head;
  
              while (tNode.next != head &amp;&amp; tNode.element &lt;= e) {
                  pNode = tNode;
                  tNode = tNode.next;
              }
  
              pNode.next = nNode;
              nNode.next = tNode;
              tNode.next=head;                                           
          }
      }
  }
  </code></pre>