title: Reverse every k block of nodes in a link list
body: <p>I was trying to solve this problem of reversing every k block of nodes in a link list. I am processing 2*k elements in every outer while loop. Can it be done by just processing k elements in every outer while loop or without using hasknodes() function?</p>
  
  <p>Sample input: 1->2->3->4->5 and k = 3</p>
  
  <p>Sample output:3->2->1->4->5</p>
  
  <pre><code> struct node *rev(struct node *head,int k)
      {
          if(k == 0 || k == 1) {
              return head;
          }
          int i;
          struct node *prev,*temp,*curr,*newhead,*p,*thead;
          p = head;
          thead = head;
          newhead = NULL;
          while(p &amp;&amp; hasknodes(p,k)) {
              prev = NULL;
              curr = p;
              i = 0;
              while(curr &amp;&amp; i &lt; k) {
                  temp = curr-&gt;next;
                  curr-&gt;next = prev;
                  prev = curr;
                  curr= temp;
                  i++;
              }
              if(newhead == NULL) {
                  newhead = prev;
              }
              else {
                  thead-&gt;next = prev;
              }
              p-&gt;next = curr;
              head = p;
              if(p) {
                  p = p-&gt;next;
              }
          }
          if(newhead == NULL) {
              return head;
          }
          return newhead;
      }
  //The function hasknodes(p,k) checks if there are k elements present from the current position p.
  </code></pre>