linked list reverse
given a list as: Header->1->2->3->4->5
reverse it, the idea should be like:
Step1:Header->2->1->3->4->5
Step2:Header->3->2->1->4->5
Step3:Header->4->3->2->1->5
Step4:Header->5->4->3->2->1
running time of such solution is obvious O(n)
coding:
need a variable to trace the node to be brought forward(tobebroughtforward);(line 5)
need reserve the node before the tobebroughtforward(position); (the variable p is it.)
we can easily see the first position value should be the first node (Header->1), the second position value should be the original second node (Header->1->2)
each iteration, connect the position and the node after tobebroughtforward,so position->next should be tobebroughtforward->next ;(line 9)
each iteration, tobebroughtforward shouldbe brought front as the first node;(line 10-11)
1: 2: void reverse(pnode list)
3: { 4: pnode p=list->next; 5: pnode tobebroughtforward=NULL;6: while(p->next!=NULL)
7: { 8: tobebroughtforward=p->next; 9: p->next=tobebroughtforward->next; 10: tobebroughtforward->next=list->next; 11: list->next=tobebroughtforward; 12: } 13: }
1: #include <stdio.h> 2: #include <stdlib.h> 3: 4: struct node;
5: typedef struct node *ptrtonode;
6: typedef ptrtonode pnode;7: struct node
8: {9: int value;
10: pnode next; 11: }; 12: pnode create_newnode();13: void append(pnode list,pnode p);
14: pnode find_previous(pnode list,pnode boy);15: void insert_before(pnode list,pnode position,pnode newboy);
16: void reverse(pnode list);
17: void print_list(pnode list);
18: 19: pnode create_newnode() 20: {21: pnode p=malloc(sizeof(struct node));
22: p->value=0;
23: p->next=NULL;24: return p;
25: } 26: 27: void append(pnode list,pnode p)
28: { 29: pnode c=list;30: while(c->next!=NULL)
31: { 32: c=c->next; 33: } 34: c->next=p; 35: } 36: 37: /*
38: if not found the previous, then return the last one
39: */
40: pnode find_previous(pnode list,pnode boy) 41: { 42: pnode p=list;43: while(p->next!=NULL && p->next->value!=boy->value)
44: { 45: p=p->next; 46: } 47: 48: return p;
49: } 50: 51: void insert_before(pnode list,pnode position,pnode newboy)
52: { 53: pnode previous=find_previous(list,position);54: if(previous==NULL)
55: {56: printf("can not find the previous node");
57: exit(-1); 58: } 59: previous->next=newboy; 60: newboy->next=position; 61: } 62: 63: void reverse(pnode list)
64: { 65: pnode p=list->next; 66: pnode tobebroughtforward=NULL;67: while(p->next!=NULL)
68: { 69: tobebroughtforward=p->next; 70: p->next=tobebroughtforward->next; 71: tobebroughtforward->next=list->next; 72: list->next=tobebroughtforward; 73: } 74: } 75: 76: void print_list(pnode list)
77: { 78: pnode c=list->next;79: while(c!=NULL && c->next!=NULL)
80: {81: printf("%d,",c->value);
82: c=c->next; 83: }84: printf("%d\n",c->value);
85: } 86: 87: int main()
88: {89: int i=0;
90: pnode list=create_newnode(); 91: pnode p;92: for(i=0;i<100;i++)
93: { 94: p=create_newnode();95: p->value=i;
96: append(list,p); 97: } 98: 99: print_list(list); 100: reverse(list); 101: 102: print_list(list);103: return 0;
104: }
at last, give a solution/codes sample from baidu,with compare i think mine is more easy to understand
:
1: void reverse2(pnode list)
2: { 3: pnode current=list->next; 4: pnode next=NULL; 5: pnode pre=NULL;6: while(current!=NULL)
7: { 8: next=current->next; 9: current->next=pre; 10: pre=current; 11: current=next; 12: } 13: list->next=pre; 14: }

浙公网安备 33010602011771号