title: How does swapping nodes in linked list works?
body: <p>Below is the code for swapping nodes without changing data. I wonder whether swapping the next pointers of nodes required? Will swapping the current nodes doesn't swap the next pointers? Why?</p>
  
  <pre><code>    void swapNodes(Node** head_ref, int x, int y) 
      { 
  
          // Nothing to do if x and y are same 
          if (x == y) 
             return; 
  
          Node **a = NULL, **b = NULL; 
  
          // search for x and y in the linked list 
          // and store therir pointer in a and b 
          while (*head_ref) { 
  
                if ((*head_ref)-&gt;data == x) { 
                     a = head_ref; 
                } 
  
                else if ((*head_ref)-&gt;data == y) { 
                     b = head_ref; 
                } 
  
                head_ref = &amp;((*head_ref)-&gt;next); 
           } 
  
           // if we have found both a and b 
           // in the linked list swap current 
           // pointer and next pointer of these 
           if (a &amp;&amp; b) { 
  
               swap(*a, *b); 
               swap(((*a)-&gt;next), ((*b)-&gt;next)); 
           } 
      } 
  
      void swap(Node*&amp; a, Node*&amp; b) 
      { 
  
           Node* temp = a; 
           a = b; 
           b = temp; 
      }
  </code></pre>
  
  <p>Thank You.</p>