title: Linked-list: bubble sorting pointers(C)
body: <p>I'm working on a university assignment. I am trying to write a linked-list sort in C. I am not allowed to swap values - only pointers.</p>
  
  <p>Here is my sorting function:</p>
  
  <pre><code>struct node *sort_list(struct node *head) {
  
  bool swapped ;
  struct node *cur = head, *first ;
  
  if ( head == NULL || head-&gt;next == NULL ) return head ;
  else {
      do {
          swapped = false ;
          while ( cur != NULL &amp;&amp; cur-&gt;next != NULL ){
              if (cur-&gt;value &gt; cur-&gt;next-&gt;value){
                  cur = swap_with_next( cur ) ;
                  swapped = true ;
              }
              cur = cur-&gt;next ;
          }   
      } while (swapped == true) ;
  
  }
  return head ;
  
  }
  </code></pre>
  
  <p>And the swap function:</p>
  
  <pre><code> struct node *swap_with_next(struct node *n) {
        struct node *tmp ;
        tmp = n ;
        n = n-&gt;next ;
        tmp-&gt;next = n-&gt;next ;
        n-&gt;next = tmp ;
        return n ;
   }
  </code></pre>
  
  <p>Problem: Incorrect output: </p>
  
  <pre><code>input:  5-&gt;2-&gt;NULL
  output: 5-&gt;NULL
  
  input:  9-&gt;1-&gt;5-&gt;2-&gt;8-&gt;3
  output: 9-&gt;5-&gt;8
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>