算法.链表反转
链表反转的思路:
lrs.h:
/************************************************************************* > File Name: lrs.h > Author: zhoulin > Mail: 715169549@qq.com > Created Time: Sun 17 Apr 2016 01:08:11 PM CST ************************************************************************/ #ifndef _LRS_H #define _LRS_H typedef struct _listNode { int v; struct _listNode *next; }listNode; //reverse a list node start with start,end whith end postion listNode *listReverse(listNode *p,int start,int end); #endif
lrs.c:
/************************************************************************* > File Name: lrs.c > Author: zhoulin > Mail: 715169549@qq.com > Created Time: Sun 17 Apr 2016 01:09:37 PM CST ************************************************************************/ #include "lrs.h" #include <stdio.h> void listPrt(listNode *p) { while(p != NULL) { if(p->next == NULL) { fprintf(stdout,"%d\n",p->v); break; } fprintf(stdout,"%d ->",p->v); p = p->next; } } /* *采用分治法 * 1.拆分链为2个链表,t_head保存目标链表的头节点,t_tail保存目标链表的最后一个节点 * 2.cur遍历到t_tail的下一个节点,n_head为t_tail下一个节点开始的新链表,采用头插法把cur节点插入到新链表,依次循环 * 3.t_tail的下一个节点为新链表的头节点,t_tail->next = n_head;最后把n_tail指向cur遍历的剩余节点。 */ listNode *listReverse(listNode *p,int start,int end) { if(start >= end) { return p; } listNode *t_head,*t_tail;//target_head为目标链表第一节点,target_cur为目标链表的最后一个节点 listNode *cur,*n_head,*n_tail;//cur为遍历p的游标节点 cur = n_head = n_tail =NULL; t_head = t_tail = NULL; int i = 1; for(;i <start;i++) { if(i == 1) { t_head = p; } if(i == start -1) { t_tail = p; } p = p->next;//find list head next node } cur = p; for(;(i <= end)&& (cur != NULL);i++) { listNode *pn = cur->next; if(n_head == NULL) { n_head = n_tail = cur; }else{ cur->next = n_head; n_head = cur; } cur = pn; } if(t_head == NULL) { t_head = n_head; t_tail = n_tail; }else{ t_tail->next = n_head; } if(n_tail != NULL) n_tail->next = cur; return t_head; } int main(void) { int i,j; for(i = 6;i>=1;i--) { for(j=1;j<=i;j++) { if(j>=i) { continue; } listNode a1,a2,a3,a4,a5,a6; a1.v =1; a2.v =2; a3.v =3; a4.v =4; a5.v =5; a6.v =6; a1.next = &a2; a2.next = &a3; a3.next = &a4; a4.next = &a5; a5.next = &a6; a6.next = NULL; listNode *tmp = &a1; fprintf(stdout,"------------------------source list----------------------------\n"); listPrt(tmp); fprintf(stdout,"------------------------listReverse(%d,%d)---------------------\n",j,i); listNode *rs = listReverse(tmp,j,i); if(rs != NULL) listPrt(rs); fprintf(stdout,"\n"); } } return 0; }
运行结果:
root@:/data/code/cwork/demo:./lrs ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(1,6)--------------------- 6 ->5 ->4 ->3 ->2 ->1 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(2,6)--------------------- 1 ->6 ->5 ->4 ->3 ->2 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(3,6)--------------------- 1 ->2 ->6 ->5 ->4 ->3 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(4,6)--------------------- 1 ->2 ->3 ->6 ->5 ->4 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(5,6)--------------------- 1 ->2 ->3 ->4 ->6 ->5 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(1,5)--------------------- 5 ->4 ->3 ->2 ->1 ->6 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(2,5)--------------------- 1 ->5 ->4 ->3 ->2 ->6 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(3,5)--------------------- 1 ->2 ->5 ->4 ->3 ->6 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(4,5)--------------------- 1 ->2 ->3 ->5 ->4 ->6 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(1,4)--------------------- 4 ->3 ->2 ->1 ->5 ->6 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(2,4)--------------------- 1 ->4 ->3 ->2 ->5 ->6 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(3,4)--------------------- 1 ->2 ->4 ->3 ->5 ->6 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(1,3)--------------------- 3 ->2 ->1 ->4 ->5 ->6 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(2,3)--------------------- 1 ->3 ->2 ->4 ->5 ->6 ------------------------source list---------------------------- 1 ->2 ->3 ->4 ->5 ->6 ------------------------listReverse(1,2)--------------------- 2 ->1 ->3 ->4 ->5 ->6