title: Sorting a Singly Linked List With Pointers
body: <p>I am trying to sort a singly linked list using bubble sort by manipulating ONLY the pointers, no keys.</p>
  
  <p>The following gets stuck in the for loop and loops infinitely. I don't understand why this is. Can anybody explain to me why the end of the list is not being found?</p>
  
  <pre><code>Node* sort_list(Node* head)
  {
      Node * temp;
      Node * curr;
      for(bool didSwap = true; didSwap; ) {
              didSwap = false;
              for(curr = head; curr-&gt;next != NULL; curr = curr-&gt;next) {
                      if(curr-&gt;key &gt; curr-&gt;next-&gt;key) {
                              temp = curr;
                              curr = curr-&gt;next;
                              curr-&gt;next = temp;
                              didSwap = true;
                      }
                      cout &lt;&lt; curr-&gt;next-&gt;key &lt;&lt; endl;
              }
      }
      return head;
  </code></pre>
  
  <p>}</p>
  
  <p>If I change the code so that the keys (data) are swapped, then the function works properly but for some reason I am not able make it work by manipulating only pointers.</p>