递归地反转链表

时间:2016-06-11 06:29:59

标签: c recursion linked-list

我的链接列表反向函数给出了正确的结果。但我是 困惑。

linked list= 12->5->4->3

as per my reverse function the result should be
4->5->12

but fortunately it produces the correct reversed list
3->4->5->12.

pls help me to understand what happening 




//head_ref  global variable



struct n* reverse(struct n *head){

    struct n *pre,*cur;  //temp variable for first and second node

    if(head->next==NULL){
        head_ref = head;   // head_ref global variable initialized with head pointer
        return;
     }
    pre = head;
    cur = head->next;
    reverse(cur);

    cur->next = pre ;
    pre->next = NULL;

 }

第一次打电话    pre = 12           cur = 5       第二个电话           pre = 5           cur = 4       第3个电话           pre = 4           cur = 3

   3->next = NULL //base condition fullfilled
   so it will exit ( 3rd call )

   reverse will start from 
   second call
      pre = 5
      cur = 4

我的反向链表应该是4-> 5-> 12

但它产生3-> 4-> 5-> 12(正确的反向链表)

为什么它会给出正确的结果。请解释????

2 个答案:

答案 0 :(得分:3)

让我们从链表12->5->4->3开始。 当你的main(或其他调用函数)调用reverse函数时,head有数据12.以下是在第四次调用reverse函数后创建的堆栈。

                 |                                           |
                 |___________________________________________|     gonna be
                 |__*head=3 *head->next=NULL__head_ref=3_____| => popped out
                 |__*head=4 pre=4 curr=3_____________________|   
                 |__*head=5 pre=5 cur=4______________________|
     main calls=>|__*head=12_pre=12 cur=5____________________| 

正如你所说,你已经清楚了上图。当我们有head-> next = NULL时,堆栈中最顶层的条目会弹出,而* head = 4,pre = 4和curr = 3的条目将成为堆栈中最顶层的条目。然后curr-> prev评估为 3-> next = 4和4-> next-> NULL。

                 |                                                      |
                 |                                                      |
    popped out=> |______________________________________________________|
                 |__*head=4 pre=4 curr=3__(3->next)=4__(4->next)=NULL___|
                 |__*head=5 pre=5 cur=4_________________________________|
                 |__*head=12_pre=12 cur=5_______________________________| 

然后输入* head = 4 pre = 4 curr = 3 __(3> next)= 4 __(4-> next)= NULL弹出并发生同样的事情。

                 |                                                      |
                 |                                                      |
                 |                                                      |
    popped out=> |______________________________________________________|
                 |__*head=5 pre=5 cur=4____(4->next)=5___(5->next=NULL)_|
                 |__*head=12_pre=12 cur=5_______________________________| 

在剩下的一个条目中,您可以看到12-> next变为NULL,因此12现在成为链表的尾部。

                 |                                                      |
                 |                                                      |
                 |                                                      |
                 |                                                      |
    popped out=> |______________________________________________________|
                 |__*head=12_pre=12 cur=5__(5->next)=12_(12->next)=NULL_| 


                 |                                                      |
                 |                                                      |
                 |                                                      |
                 |                                                      |
                 |                                                      |
    popped out=> |______________________________________________________| 

堆栈变空,你的最终链表有3个作为头部,12个作为尾部:3->4->5->12

答案 1 :(得分:2)

  

3-> next = NULL //基本条件已满,因此它将退出(第3个   致电)

     

反向将从第二次调用开始         pre = 5         cur = 4

从这里没有反向开始

  

调用pre = 4 cur = 3

你叫反向(3)什么也不做, 并执行行

cur->next = pre ;
pre->next = NULL;

其中cur = 3且pre = 4,所以你得到 3-> 4