title: How can I implement a function to return true if array or arrayList refers to a previous element?
body: <p>A playlist is considered a repeating playlist if any of the songs contain a reference to a previous song in the playlist. Otherwise, the playlist will end with the last song which points to null.</p>
  
  <p>I need to Implement a function isRepeatingPlaylist that, returns true if a playlist is repeating or false if it is not.</p>
  
  <p>For example, the following code prints "true" as both songs point to each other.</p>
  
  <pre><code>Song first = new Song("Hello");
  Song second = new Song("Eye of the tiger");    
  first.setNextSong(second);
  second.setNextSong(first);    
  System.out.println(first.isRepeatingPlaylist());
  </code></pre>
  
  <p>Again, this is not a homework exercise, I am doing coding challenges because when I read theory about programming concepts, I can almost understand, but when faced with writing a program I don't know where to start, or how to apply.           </p>
  
  <pre><code>public class Song {
  
      private String name;
      private Song nextSong;
  
      public Song(String name) {
          this.name = name;
      }
  
      public void setNextSong(Song nextSong) {
          this.nextSong = nextSong;
      }
  
      public boolean isRepeatingPlaylist() {
          //throw new UnsupportedOperationException("Waiting to be implemented.");
          List&lt;String&gt; list = new ArrayList&lt;String&gt;();
          list.add(one);
          list.add(two);
          list.add(three);
          list.add(four);
  
          if list.contains()
          return true;
          else
              return false;
  
      }
  
      public static void main(String[] args) {
          Song first = new Song("Hello");
          Song second = new Song("Eye of the tiger");
          Song third = new Song("a test");
          Song fourth = new Song("survivor");
  
          first.setNextSong(second);
          second.setNextSong(first);
  
          System.out.println(first.isRepeatingPlaylist();
      }
  }
  </code></pre>