title: reversing a linked list in groups of a given size
body: <p>I am solving a problem of reversing a linked list in groups of given size and the algorithm i am using is as follows:</p>
  
  <p>1) Reverse the first sub-list of size k. While reversing i kept the track of the next node and previous node. Let the pointer to the next node be next and pointer to the previous node be prev &amp; pointer to current node is ptr.</p>
  
  <p>2) head = reverse(next, k)-Recursively call for rest of the list </p>
  
  <p>3) return prev which is the new head of the reversed list</p>
  
  <p>My code sample is:</p>
  
  <pre><code>struct node *reverse(struct node *start,int k)
  {
      struct node *prev,*ptr,*next;
      prev=NULL;
      ptr=start;
      int count=0;
      while(count&lt;k &amp;&amp; ptr!=NULL)
      {
          next=ptr-&gt;link;
          ptr-&gt;link=prev;
          prev=ptr;
          ptr=next;
          count++;
      }
  
      if(next!=NULL)//recursive call
      start=reverse(next,k);
  
      return prev;
  }
  </code></pre>
  
  <p>But my output is only reversing the first half of the list!</p>
  
  <p>for eg: If my list is : 98 74 94 857 8 7</p>
  
  <pre><code>    My output is : 94 74 98(The rest is not being displayed)
  </code></pre>
  
  <p>Where am i wrong?..Is this method correct?</p>