title: Circular LinkedList in Java
body: <p>I am brushing up  on my data structures by reading a book and one of the questions it asks is to build a circular Single Linked List by not using "first" &amp; "last" pointers, but rather allow access to it by using one reference "current". I am not sure I understand the question, I always thought I needed at least either first or last. Here is my implementation but it has "first", not sure how to get around it. Can you please comment on how I can adjust my code to eliminate reliance on first?</p>
  
  <pre><code>class Link {
      public int iData;              
      public Link next;              
  
      public Link(int id) { // constructor
          iData = id;                         
      }                          
  
      public void displayLink() {
          System.out.print(iData + " ");
      }
  }  // end class Link
  </code></pre>
  
  <p>Then here's the list itself:</p>
  
  <pre><code>public class CircularLinkedList {
      private Link first;
      private Link current;    
  
      public Link getCurrent(){
          return current;
      }
  
      public void setCurrent(int data){
  
      }
  
      public void advance(){
          current = current.next;
      }
  
      public void insert(int data) {
          Link newLink = new Link(data);
          if (first == null) { 
              first = current = newLink;
          } else {
              current.next = newLink;
          }
          current = newLink;
          newLink.next = first;
      }
      ...
  }
  </code></pre>