【剑指offer】递归循环两种方式反转链表

http://blog.csdn.net/ns_code/article/details/25737023

转载请注明出处:http://blog.csdn.net/ns_code/article/details/25737023

 

    本文分别用非递归和递归两种方式实现了链表的反转,在九度OJ上AC。

 

题目描述:

输入一个链表,反转链表后,输出链表的所有元素。
(hint : 请务必使用链表)

 

输入:

输入可能包含多个测试样例,输入以EOF结束。
对于每个测试案例,输入的第一行为一个整数n(0<=n<=1000):代表将要输入的链表的个数。
输入的第二行包含n个整数t(0<=t<=1000000):代表链表元素。

 

输出:

对应每个测试案例,
以此输出链表反转后的元素,如没有元素则输出NULL。

 

样例输入:
5
1 2 3 4 5
0
样例输出:
5 4 3 2 1
NULL

    很明显,翻转后,尾节点和头结点互换了。

 

    我们需要设置三个指针,分别指向当前要反转的节点、当前要反转节点的前一个节点、当前要反转节点的下一个节点。要注意链表为空,以及只有一个头结点的情况。

    非递归实现如下:

 

[cpp] view plain copy
 
  1. /* 
  2. 反转链表,返回翻转后的头结点 
  3. */  
  4. pNode ReverseList(pNode pHead)  
  5. {  
  6.     if(pHead == NULL)  
  7.         return NULL;  
  8.     if(pHead->next == NULL)  
  9.         return pHead;  
  10.   
  11.     pNode pCur = pHead;  
  12.     pNode pPre = NULL;  
  13.     while(pCur != NULL)  
  14.     {  
  15.         pNode pNext = pCur->next;  
  16.         pCur->next = pPre;  
  17.         pPre = pCur;  
  18.         pCur = pNext;  
  19.     }  
  20.     return pPre;  
  21. }  

    递归实现如下:

 

 

[cpp] view plain copy
 
  1. /* 
  2. 递归实现反转链表,返回翻转后的头结点 
  3. */  
  4. pNode ReverseListRecursivly(pNode pPre,pNode pCur)  
  5. {  
  6.     if(pCur == NULL)  
  7.         return NULL;  
  8.     if(pCur->next == NULL)  
  9.     {  
  10.         pCur->next = pPre;  
  11.         return pCur;  
  12.     }  
  13.   
  14.     pNode pNext = pCur->next;  
  15.     pCur->next = pPre;  
  16.     pNode pNewHead = ReverseListRecursivly(pCur,pNext);  
  17.     return pNewHead;  
  18. }  
  19.   
  20. pNode ReverseList2(pNode pHead)  
  21. {  
  22.     return ReverseListRecursivly(NULL,pHead);  
  23. }  


    根据题目要求,测试代码如下:

 

 

[cpp] view plain copy
 
  1. int main()  
  2. {  
  3.     int n;  
  4.     while(scanf("%d",&n) != EOF)  
  5.     {  
  6.         pNode pHead = NULL;  
  7.         if(n > 0)  
  8.         {  
  9.             int i,data;  
  10.             scanf("%d",&data);  
  11.             pHead =(pNode)malloc(sizeof(Node));  
  12.             if(pHead == NULL)  
  13.                 exit(EXIT_FAILURE);  
  14.             pHead->data = data;  
  15.             pHead->next = NULL;  
  16.   
  17.             pNode pCur = pHead;  
  18.             for(i=0;i<n-1;i++)  
  19.             {  
  20.                 scanf("%d",&data);  
  21.                 pNode pNew =(pNode)malloc(sizeof(Node));  
  22.                 if(pNew == NULL)  
  23.                     exit(EXIT_FAILURE);  
  24.                 pNew->data = data;  
  25.                 pNew->next = NULL;  
  26.                 pCur->next = pNew;  
  27.                 pCur = pCur->next;  
  28.             }  
  29.         }  
  30.   
  31.         pNode pNewHead = ReverseList2(pHead);  
  32.         if(pNewHead == NULL)  
  33.             printf("NULL\n");  
  34.         else  
  35.         {  
  36.             pNode pCur = pNewHead;  
  37.             while(pCur != NULL)  
  38.             {  
  39.                 //这里主要时要注意输出的格式  
  40.                 if(pCur->next == NULL)  
  41.                     printf("%d\n",pCur->data);  
  42.                 else  
  43.                     printf("%d ",pCur->data);  
  44.                 pCur = pCur->next;  
  45.             }  
  46.         }  
  47.     }  
  48.     return 0;  
  49. }  

 

/**************************************************************
    Problem: 1518
    User: mmc_maodun
    Language: C
    Result: Accepted
    Time:150 ms
    Memory:2364 kb
 
 
===================================================
  1. ActList* ReverseList2(ActList* head)  
  2. {  
  3.     //ActList* temp=new ActList;  
  4.  if(NULL==head|| NULL==head->next) return head;    //少于两个节点没有反转的必要。  
  5.     ActList* p;  
  6.     ActList* q;  
  7.     ActList* r;  
  8.     p = head;    
  9.     q = head->next;  
  10.     head->next = NULL; //旧的头指针是新的尾指针,next需要指向NULL  
  11.     while(q){  
  12.         r = q->next; //先保留下一个step要处理的指针  
  13.         q->next = p; //然后p q交替工作进行反向  
  14.         p = q;   
  15.         q = r;   
  16.     }  
  17.     head=p; // 最后q必然指向NULL,所以返回了p作为新的头指针  
  18.     return head;      
  19. }  

posted on 2017-08-23 21:19  小西红柿  阅读(265)  评论(0)    收藏  举报

导航