开张了

中午蛋疼,再做一遍华为面试题,顺便立帖严重BS下面试那天自己写的作答!

很简单的代码,没有注释。

代码
1 struct listNode
2 {
3 int a;
4 listNode * pNext;
5 };
6
7 listNode* makeList(int len)
8 {
9 listNode* pCreator = NULL;
10 listNode* pRoller = (listNode*)malloc(sizeof(listNode));
11 listNode* pHead = pRoller;
12 pHead->a =0;
13 pHead->pNext = NULL;
14
15 for (int i=1; i<len; ++i)
16 {
17 pCreator = (listNode*)malloc(sizeof(listNode));
18 pCreator->a=i;
19 pCreator->pNext = NULL;
20 pRoller->pNext = pCreator;
21 pRoller = pCreator;
22 }
23
24 return pHead;
25 }
26
27  void freeList(listNode* pHead)
28 {
29 listNode* pPrev=pHead;
30 listNode* pNext=NULL;
31
32 while (pPrev != NULL)
33 {
34 pNext = pPrev->pNext;
35 free(pPrev);
36 pPrev = pNext;
37 }
38 }
39
40 void printList(listNode* pHead)
41 {
42 while (pHead!=NULL)
43 {
44 printf("%d\n", pHead->a);
45 pHead = pHead->pNext;
46 }
47 }
48
49 void reverseList(listNode** pHead)
50 {
51 listNode* pPrev = NULL;
52 listNode* pNext = NULL;
53 listNode* pCurr = NULL;
54
55 if ((*pHead)==NULL || (*pHead)->pNext==NULL)
56 {
57 return;
58 }
59
60 pPrev =*pHead;
61 pCurr = pPrev->pNext;
62 pPrev->pNext = NULL;//尾置空
63
64 while (pCurr!=NULL && pCurr->pNext!=NULL)
65 {
66 pNext = pCurr->pNext;
67 pCurr->pNext = pPrev;
68 pPrev = pCurr;
69 pCurr = pNext;
70 }
71
72 pCurr->pNext = pPrev;
73 *pHead = pCurr;
74 }
75
76 int main()
77 {
78 listNode * head = makeList(15);
79 printList(head);
80 reverseList(&head);
81 printList(head);
82 freeList(head);
83 head = NULL;
84 return0;
85 }

 

posted @ 2010-03-25 13:43  ruobent  阅读(233)  评论(0编辑  收藏  举报