title: How do I shuffle nodes in a linked list?
body: <p>I just started a project for my Java2 class and I've come to a complete stop. I just can't get
  my head around this method. Especially when the assignment does NOT let us use any other DATA STRUCTURE or shuffle methods from java at all. </p>
  
  <p>So I have a Deck.class in which I've already created a linked list containing 52 nodes that hold 52 cards.</p>
  
  <pre><code>public class Deck {
  
      private Node theDeck;
      private int numCards;
  
      public Deck () 
      {
          while(numCards &lt; 52)
          {
              theDeck = new Node (new Card(numCards), theDeck);
              numCards++;
          }
      }
  
      public void shuffleDeck()
      {           
          int rNum;
          int count = 0;
          Node current = theDeck;
          Card tCard;
          int range = 0;  
  
          while(count != 51)
          {   
              // Store whatever is inside the current node in a temp variable
                 tCard = current.getItem();
  
              // Generate a random number between 0 -51      
                  rNum = (int)(Math.random()* 51);
  
              // Send current on a loop a random amount of times
                 for (int i=0; i &lt; rNum; i ++)
                  current = current.getNext();   ******&lt;-- (Btw this is the line I'm getting my error, i sort of know why but idk how to stop it.)
  
              // So wherever current landed get that item stored in that node and store it in the first on
              theDeck.setItem(current.getItem());
  
              // Now make use of the temp variable at the beginning and store it where current landed
              current.setItem(tCard);
  
              // Send current back to the beginning of the deck
              current = theDeck;
  
              // I've created a counter for another loop i want to do     
              count++;
  
              // Send current a "count" amount of times for a loop so that it doesn't shuffle the cards that have been already shuffled.   
              for(int i=0; i&lt;count; i++)
               current = current.getNext();  ****&lt;-- Not to sure about this last loop because if i don't shuffle the cards that i've already shuffled it will not count as a legitimate shuffle? i think? ****Also this is where i sometimes get a nullpointerexception****
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>Now I get different kinds of errors 
  When I call on this method:</p>
  
  <ul>
  <li><p>it will sometimes shuffle just 2 cards but at times it will shuffle 3 - 5 cards then give me a NullPointerException.
  I've pointed out where it gives me this error with asterisks in my code above</p></li>
  <li><p>at one point I got it to shuffle 13 cards but then everytime it did that it didn't quite shuffle them the right way. one card kept always repeating. </p></li>
  <li><p>at another point I got all 52 cards to go through the while loop but again it repeated one card various times.</p></li>
  </ul>
  
  <p>So I really need some input in what I'm doing wrong. Towards the end of my code I think my logic is completely wrong but I can't seem to figure out a way around it. </p>