title: Circular LinkedList implementation in Java
body: <p>This is an assignment. I have to create a circular linked list and remove every third number in the list. When my program reaches the end of the list it should go back to the head and continue the process until only one number remains.</p>
  
  <p>I've searched online and some other reference books but couldn't solve my problem. Most of the references that I've found say things such as:</p>
  
  <blockquote>
    <p>Beside the fact that circular lists have no end, they are quite the same as regular lists</p>
  </blockquote>
  
  <p>or (taken from my textbook):</p>
  
  <blockquote>
    <p>A singly-linked list is circularly linked if the successor of the last node is the first</p>
  </blockquote>
  
  <p>But these don't tell how to do it. I've also tried using some code I found on this site, but that did not clear anything up.</p>
  
  <p>I can get as far as creating a list (I don't know if it's a circular linked list) and displaying it, but the order of the elements is strange: </p>
  
  <ul>
  <li>if the list has 6 numbers, the list will be 1,6,5,4,3,2.</li>
  <li>if the list has 8 numbers, the list will be 1,8,7,6,5,4,3,2.</li>
  </ul>
  
  <p>Without getting a correct list, I can do the deletion properly. What is wrong with the following code:</p>
  
  <pre><code>public class LastNumberDemo {
      public static void main(String[] args) {
          LastNumberNode ll=new LastNumberNode();
          System.out.println("how long is the list: ");
          Scanner keyboard = new Scanner(System.in);
          int input = keyboard.nextInt();
  
          if(input&lt;=0) {
              System.out.println("no number to creat list");
          }
          if(input==1) {
              System.out.println("The Last number is 1.");
          }
          else {
              String[] n=new String[input];
              for(int index=0; index&lt;n.length; index++)
                  n[index]=Integer.toString(index+1);
              for(String e:n)
                  ll.add(e);
              System.out.print("The list contains: \n");
              ll.print();
              System.out.print("\nThe last number is: ");
              ll.remove();
              ll.print();
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>//The circular linked list class
  class LastNumberNode{
      private class Node{
          String value;  
          Node next;     
  
          Node(String val, Node n){
              value = val;
              next = n;
          }
  
          Node(String val){
              value=val;
              next=null;
          }
      } //This brace was missing - Edd
  
      private Node first;
  
      public LastNumberNode(){
        first = null;
      }
  
      public boolean isEmpty(){        
         return first == null;
      }
  
      public int size(){
          int count = 0;
          Node p = first.next;   
          while (p != first){
              count ++;
              p = p.next;
          }
          return count;
      }
  
      public void add(String e) {
          Node p=new Node(e);
          if(first==null){
              first=p;
              first.next=first;
          }
          else{
              first.next=new Node(e,first.next);
          }
      }
  
      public void remove(){
          while(size()&gt;0){
              Node target=first.next.next;   
              Node temp=first;               
              target=target.next;          
              last.next=temp;
              first=target;
          }
      }
  
      public void print(){
          Node ref=first;
          for(int index=-1; index&lt;size();index++)
              System.out.print(ref.value+" ");
          ref=ref.next;
      }
  } //Extra brace removed - Edd
  </code></pre>