单链表逆置

 1 #include "stdafx.h"
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <iostream>
 5 typedef struct stData
 6 {
 7     int n;
 8     stData* next;
 9 }*pData;
10 
11 pData ReverseList(pData pHead)
12 {
13     if (pHead == NULL||pHead->next == NULL)
14     {
15         return pHead;
16     }
17 
18     pData newHead = ReverseList(pHead->next);
19 
20     pHead->next->next = pHead;
21     pHead->next = NULL;
22 
23     return newHead;
24 }
25 
26 int main()
27 {
28 
29     pData pHead = NULL;
30     for (size_t i = 0; i < 10; i++)
31     {
32         pData p = new stData();
33         p->n = i;
34         p->next = pHead;
35         pHead = p;
36     }
37     pHead = ReverseList(pHead);
38     
39     while (pHead != NULL)
40     {
41         printf("%d",pHead->n);
42         pHead = pHead->next;
43         std::cout << " ";
44     }
45 
46     
47 
48     system("pause");
49     return 0;
50 }

 

posted @ 2018-03-07 14:21  风的哀伤  阅读(127)  评论(0)    收藏  举报