title: Java Using Nodes with LinkedList
body: <p>I've been working through some standard coding interview questions from a book I recently bought, and I came across the following question and answer:</p>
  
  <p>Implement an algorithm to find the nth to last element in a linked list.</p>
  
  <p>Here's the provided answer:</p>
  
  <pre><code>public static LinkedListNode findNtoLast(LinkedListNode head, int n) { //changing LinkedListNode to ListNode&lt;String&gt;
          if(head == null || n &lt; 1) {
              return null;
          }
          LinkedListNode p1 = head;
          LinkedListNode p2 = head;
          for(int j = 0; j &lt; n-1; ++j) {
              if(p2 == null) {
                  return null;
              }
              p2 = p2.next;
          }
          if(p2 == null) {
              return null;
          }
          while(p2.next != null) {
              p1 = p1.next;
              p2 = p2.next;
          }
          return p1;
      }
  </code></pre>
  
  <p>I understand the algorithm, how it works, and why the book lists this as its answer, but I'm confused about how to access the LinkedListNodes to send as an argument to the method.  I know that I'd have to create a LinkedListNode class (since Java doesn't already have one), but I can't seem to figure out how to do that.  It's frustrating because I feel like I should know how to do this.  Here's something that I've been working on.  I'd greatly appreciate any clarification.  You can expand/comment on my code or offer your own alternatives.  Thanks.</p>
  
  <pre><code>class ListNode&lt;E&gt; {
      ListNode&lt;E&gt; next;
      E data;
  
      public ListNode(E value) {
          data = value;
          next = null;
      }
  
      public ListNode(E value, ListNode&lt;E&gt; n) {
          data = value;
          next = n;
      }
  
      public void setNext(ListNode&lt;E&gt; n) {
          next = n;
      }
  }
  
  public class MyLinkedList&lt;E&gt; extends LinkedList {
      LinkedList&lt;ListNode&lt;E&gt;&gt; list;
      ListNode&lt;E&gt; head;
      ListNode&lt;E&gt; tail;
      ListNode&lt;E&gt; current;
      ListNode&lt;E&gt; prev;
  
      public MyLinkedList() {
          list = null;
          head = null;
          tail = null;
          current = null;
          prev = null;
      }
  
      public MyLinkedList(LinkedList&lt;E&gt; paramList) {
          list = (LinkedList&lt;ListNode&lt;E&gt;&gt;) paramList; //or maybe create a loop assigning each ListNode a value and next ptr
          head = list.getFirst();
          tail = list.getLast(); //will need to update tail every time add new node
          current = null;
          prev = null;
      }
  
      public void addNode(E value) {
          super.add(value);
          //ListNode&lt;E&gt; temp = tail;
          current = new ListNode&lt;E&gt;(value);
          tail.setNext(current);
          tail = current;
      }
  
      public LinkedList&lt;ListNode&lt;E&gt;&gt; getList() {
          return list;
      }
  
      public ListNode&lt;E&gt; getHead() {
          return head;
      }
  
      public ListNode&lt;E&gt; getTail() {
          return tail;
      }
  
      public ListNode&lt;E&gt; getCurrent() {
          return current;
      }
  
      public ListNode&lt;E&gt; getPrev() {
          return prev;
      }
  
  
  }
  </code></pre>
  
  <p>How can the LinkedListNode head from a LinkedList?  </p>
  
  <p>Update: I think part of my confusion comes from what to put in the main method.  Do I need to create a LinkedList of ListNode?  If I do that, how would I connect the ListNodes to each other?  How would I connect them without using a LinkedList collection object?  If someone could show me how they would code the main method, I think that would put things into enough perspective for me to solve my issues.  Here's my latest attempt at the main method:</p>
  
  <pre><code>public static void main(String args[]) {
          LinkedList&lt;ListNode&lt;String&gt;&gt; list = new LinkedList&lt;ListNode&lt;String&gt;&gt;();
          //MyLinkedList&lt;ListNode&lt;String&gt;&gt; list = new MyLinkedList(linkedList);
          list.add(new ListNode&lt;String&gt;("Jeff"));
          list.add(new ListNode&lt;String&gt;("Brian"));
          list.add(new ListNode&lt;String&gt;("Negin"));
          list.add(new ListNode&lt;String&gt;("Alex"));
          list.add(new ListNode&lt;String&gt;("Alaina"));
          int n = 3;
          //ListIterator&lt;String&gt; itr1 = list.listIterator();
          //ListIterator&lt;String&gt; itr2 = list.listIterator();
          LinkedListNode&lt;String&gt; head = new LinkedListNode(list.getFirst(), null);
          //String result = findNtoLast(itr1, itr2, n);
          //System.out.println("The " + n + "th to the last value: " + result);
          //LinkedListNode&lt;String&gt; nth = findNtoLast(list.getFirst(), n);
          ListNode&lt;String&gt; nth = findNtoLast(list.getFirst(), n);
          System.out.println("The " + n + "th to the last value: " + nth);
      }
  </code></pre>
  
  <p>In an attempt to connect the nodes without using a custom linked list class, I have edited my ListNode class to the following:</p>
  
  <pre><code>class ListNode&lt;E&gt; {
      ListNode&lt;E&gt; next;
      ListNode&lt;E&gt; prev; //only used for linking nodes in singly linked list
      ListNode&lt;E&gt; current; //also only used for linking nodes in singly linked list
      E data;
      private static int size = 0;
  
      public ListNode() {
          data = null;
          next = null;
          current = null;
          if(size &gt; 0) { //changed from prev != null because no code to make prev not null
              prev.setNext(this);
          }
          size++;
      }
      public ListNode(E value) {
          data = value;
          next = null;
          current = this;
          System.out.println("current is " + current);
          if(size &gt; 0) {
              prev.setNext(current);//this line causing npe
          }
          else
          {
              prev = current;
              System.out.println("prev now set to " + prev);
          }
          size++;
          System.out.println("after constructor, size is " + size);
      }
  
      public ListNode(E value, ListNode&lt;E&gt; n) {
          data = value;
          next = n;
          current = this;
          if(size &gt; 0) {
              prev.setNext(this);
          }
          size++;
      }
  
      public void setNext(ListNode&lt;E&gt; n) {
          next = n;
      }
  }
  </code></pre>
  
  <p>As is right now, the program will run until it reaches prev.setNext(current); in the single argument constructor for ListNode.  Neither current nor prev are null at the time this line is reached.  Any advice would be greatly appreciated.  Thanks.</p>