[笔试面试][code_by_hand]反转链表

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 struct Node
 4 {
 5         int value;
 6         Node * next;
 7 };
 8 
 9 Node * reverse(Node * head)
10 {
11         Node * ret = NULL;
12         while (head != NULL)
13         {
14                 Node * temp = head->next;
15                 head->next = ret;
16                 ret = head;
17                 head = temp;
18         }
19         return ret;
20 }
21 
22 void print(Node * head)
23 {
24         while (head)
25         {
26                 printf("%d ", head->value);
27                 head = head->next;
28         }
29         printf("\n");
30 }
31 
32 int main()
33 {
34         Node nrr[100];
35         int i;
36         for(i=0; i<100; i++)
37         {
38                 nrr[i].value = i;
39                 nrr[i].next = NULL;
40         }
41         for (i=0; i<99; i++)
42         {
43                 nrr[i].next = &nrr[i+1];
44         }
45         Node * temp = reverse(&nrr[0]);
46         print(temp);
47 }

 

posted @ 2013-05-08 16:56  weiwei5987  阅读(160)  评论(0编辑  收藏  举报