title: Cracking the Coding Interview, 6th Edition- Q: 2.2
body: <p>The question is return the kth to last element of a singly linked list. All the proposed solutions are pretty complex, and I don't know why my solution is invalid. Could someone please let me know why?</p>
  
  <pre><code>public class CrackTheInterview {
  
  
      /**
       * @param args the command line arguments
       */
  
      public static Node kthToLast(Node n, int k) //pass in LinkedList's head node
      {
          int counter = 1;
          while (n.next != null)
          {
              n = n.next;
  
  
          }
          //at the end of the while loop, n equals the last node in the LinkedList
          while (counter != k)
          {
              n = n.previous;
              counter++;
          }
          //now node n is the kth node from the end
  
          return n;
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>class Node
  {
      Node next = null;
      Node previous = null;
      int data;
  
      public Node (int d)
      {
          this.data = d;
      }
  
  }
  </code></pre>