//Reverse linklist in block
//eg. block size is 3 and list is
//1 2 3 4 5 6 7 8
//Output: 3 2 1 6 5 4 8 7
LNK_NODE* ReverseByBlock(LNK_NODE* pHead ,int nBlockSize)
#include <iostream> using namespace std; //1 2 3 4 5 6 7 8 struct Node { int data; Node *next; }; Node *genLink() { Node head; Node *pt = &head; for (int i = 1; i < 9 ; ++i) { pt->next = new Node; pt->next->data = i; pt->next->next = NULL; pt = pt->next; } return head.next; } void rmLink(Node *head) { while(head) { Node *t = head; head = head->next; delete t; } } void print(Node *head) { while (head) { cout<<head->data<<" "; head = head->next; } } Node *reverseByBlock(Node *head, int n) { if (n <= 1) return head; Node root; Node *tail = &root; Node *fir = head; while (fir) { Node *sec = NULL; int i = 0; Node *nextTail = fir; while (fir && i <n) { Node *t = fir; fir = fir->next; t->next = sec; sec = t; ++i; } tail->next = sec; tail = nextTail; } return root.next; } int main() { Node *head = genLink(); print(head); cout<<endl; Node *rh = reverseByBlock(head,1); print(rh); rmLink(rh); }