title: Sort an array and put it in a linked list
body: <p>I have an algorithm in pseudocode which takes an unsorted array as input and puts it sorted in a linked list. I don't understand very well how it works. Maybe there's an error in it. I've been trying for more than an hour to understand how does it work but I'm stuck at some point. I'll explain as much as I understand. Anyone who knows how it works is pleased to help. </p>
  
  <pre><code>ORDER(A)
  head [L1] = create();    //Null List is created.
  key[head[L1]] = A[1];    //First element of the array is put as the first in list.
  
  for i=2 to length(A);
      x = create();        // create a node for every element of the array
      key[x] = A[i];       // put A[i] as key in the node.
      p1 = head[L1];       // make p1 head of the list.
      if key[p1] &gt; A[i];       //compare A[i] with the head
         insert_head(x);      // if smaller put it in the beggining and make it head
      while key[next[p1]] &lt; A[i] AND next[p1] =/= nil  // This part I don't understand 
         if next[p1] == nil                               //very well.
            next[p1] = x
            next[x] = nil
         else
            next[x] = next[p1]
            next[p1] = x;
  </code></pre>