How to shuffle or randomize a linked list
<p>i'm trying to shuffle or randomize a LinkedList of images in this case but the way i've set it up it seems to go on indefinetely </p>

<p>the shuffling is fairly simple, you have the list, note the last entry, then take the first entry and put it at a random spot in the list, and then the next first entry, etc. until the top entry is the entry you noted as the last, you put that entry in a random place in the list and the list is shuffled.</p>

<p>here's my code:</p>

<pre><code>class ShuffleClass
{
    private LinkedList&lt;Image&gt; library;
    private Image lastCard;
    private Image topCard;
    private Random rng;
    private int place;
    private LinkedListNode&lt;Image&gt; node;

    public LinkedList&lt;Image&gt; shuffle(LinkedList&lt;Image&gt; library)
    {
        this.library = library;
        lastCard = library.Last.Value;
        rng = new Random();

        while (library.First.Value != lastCard)
        {
            topCard = library.First.Value;
            library.RemoveFirst();
            place = rng.Next(1,library.Count+1);

            if (place == library.Count)
            {
                library.AddBefore(library.Last, topCard);
            }
            else
            { 
                node = library.Find(library.ElementAt(place));
                library.AddBefore(node, topCard);
            }

        }
        topCard = library.First.Value;
        library.RemoveFirst();
        place = rng.Next(0,library.Count+1);
        if(place == library.Count)
        {
            library.AddBefore(library.Last, topCard);
        }
        else
        {
            node = library.Find(library.ElementAt(place));
            library.AddBefore(node, topCard);
        }
        return library;
    }
}
</code></pre>