反转链表 [英] reversing linked list

查看:120
本文介绍了反转链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hiii,

我正在尝试使用递归来反转链表,并为其编写了以下代码。列表是开头列表的开头。

I am trying to reverse a linked list using recursion and wrote the following code for it.The list is start of the list at the beginning.

 node *reverse_list_recursive(node *list)
  {
      node *parent = list;
      node *current = list->next;

      if(current == NULL)
       return parent;

      else
       {
           current = reverse_list_recursive(current);
           current->next = parent;
           printf("\n %d  %d \n",current->value,parent->value);
           return parent;
       }

  }

我可以看到所有的链接正在扭转然而,当我尝试显示,我得到一个无限的打印的数字。我怀疑一个错误,当我试图扭转链接的第一个数字,最初在列表中。
我试过但无法想出来。请帮忙。

I could see that all the links are getting reversed. However when i try to display , i get an infinte prints of the numbers.I suspect an error when i am trying to reverse the link for the first number originally in the list. I tried but could not figure out.Please help.

谢谢

推荐答案

假设我有一个链表: / p>

Suppose I have a linked list:

 ----------      ----------      ----------      ---------- 
|  1  |    |--->|  2  |    |--->|  3  |    |--->|  4  |    |--->NULL
 ----------      ----------      ----------      ---------- 

您的代码将其转换为:

   ----------------------          ----------------------
   |                    |          |                    |
   v                    |          v                    |
 ----------      ----------      ----------      ----------
|  1  |    |--->|  2  |    |    |  3  |    |    |  4  |    | 
 ----------      ----------      ----------      ---------- 
                   ^                    |
                   |                    |
                   ----------------------

请注意,第一个元素仍然指向2。

Notice that the first element still points back to 2.

如果添加行 parent-> next = NULL 前两个,你会得到:

If you add the line parent->next = NULL after the first two, you will get:

           ----------------------          ----------------------
           |                    |          |                    |
           v                    |          v                    |
         ----------      ----------      ----------      ----------
NULL<---|  1  |    |    |  2  |    |    |  3  |    |    |  4  |    | 
         ----------      ----------      ----------      ---------- 
                           ^                    |
                           |                    |
                           ----------------------

其实是正确的结构。

完整的代码是:(您只需要打印每个递归调用的当前值)


The complete code is: (You only need to print the current value for each recursive call)

node *reverse_list_recursive(node *list)
  {
      node *parent = list;
      node *current = list->next;

      if(current == NULL)
       return parent;

      else
       {
           current = reverse_list_recursive(current);
           parent->next = NULL;
           current->next = parent;
           printf("\n %d \n",current->value);
           return parent;
       }

  }

这篇关于反转链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆