Swapping adjacent nodes of a linked list I just have a small question.  The code I wrote works well for the problem. But is it the best way, or can we make it better?

struct node * swapAdjacent(struct node * list)
{
   struct node * temp,*curr,*nextNode;
   temp = list;
   curr = temp-&gt;next;
   if(curr == NULL)
      return temp;
   nextNode = curr-&gt;next;
   curr-&gt;next = temp;
   if(nextNode == NULL) 
   {
       temp-&gt;next = nextNode;
       return curr;
   }
   temp-&gt;next = swapAdjacent(nextNode);
   return curr;
}
Consider having swapAdjacent(NULL) return NULL.  Once it does, you can get rid of the whole if (nextNode == NULL) statement, and just unconditionally say temp-&gt;next = swapAdjacent(nextNode);.
You might want to use a couple of guard clauses to separate the null checks from the other stuff.  That can make the steps easier to follow.


With those things done:

struct node * swapAdjacent(struct node * list) {
   struct node *temp, *curr, *nextNode;

   if (!list) return NULL;
   if (!list-&gt;next) return list;

   temp = list;
   curr = list-&gt;next;
   nextNode = curr-&gt;next;

   curr-&gt;next = temp;
   temp-&gt;next = swapAdjacent(nextNode);
   return curr;
}



The meanings of the names curr, temp, and nextNode are a bit foggy.  I'd change the names to something that unambiguously refers to the nodes' positions in the list either before or after the swap.
Frankly, temp and nextNode could probably go away, and the resulting code would be simpler for it.


Watch:

struct node * swapAdjacent(struct node * list) {
   if (!list) return NULL;
   if (!list-&gt;next) return list;

   struct node *newHead = list-&gt;next;
   list-&gt;next = swapAdjacent(newHead-&gt;next);
   newHead-&gt;next = list;

   return newHead;
}


Note how there's a bit of a rotation of values going on.  It was going on in the original code, too...but it was harder to see with the variables in the way.