在c中递归地反转链表

时间:2012-12-29 10:25:35

标签: c recursion linked-list singly-linked-list

当head作为参数发送给它时,以下代码可以正常工作。由于我是C的新手,我无法理解它是如何工作的。请帮帮我。

struct node *recursiveReverseLL(struct node *list)
{
    struct node *revHead;
    if (list == NULL || list->link == NULL)
    {
        return list;
    }

    revHead = recursiveReverseLL(list->link);
    list->link->link = list;
    list->link = NULL; 

    return revHead;
}

我不知道如何使用这些递归调用提供链接。 ie)如果链接是,

1 -> 2 -> 3 -> 4 

然后hw被改为,

4 -> 3 -> 2 -> 1

9 个答案:

答案 0 :(得分:55)

一般的递归算法是:

  1. Divide 2部分中的列表 - 首先 节点和列表的其余部分。
  2. 递归调用rest的反向 链表。
  3. rest链接到first
  4. 修复head指针
  5. 以下是带内联注释的代码:

    struct node* recursiveReverseLL(struct node* first){
    
       if(first == NULL) return NULL; // list does not exist.
    
       if(first->link == NULL) return first; // list with only one node.
    
       struct node* rest = recursiveReverseLL(first->link); // recursive call on rest.
    
       first->link->link = first; // make first; link to the last node in the reversed rest.
    
       first->link = NULL; // since first is the new last, make its link NULL.
    
       return rest; // rest now points to the head of the reversed list.
    }
    

    我希望这张照片能让事情更清晰:

    image http://geeksforgeeks.org/wp-content/uploads/2009/07/Linked-List-Rverse.gif

答案 1 :(得分:5)

替代解决方案:

struct node *head;
void reverse(struct node *prev, struct node *cur)
{
   if(cur){
      reverse(cur,cur->link);
      cur->link = prev;
    }
    else{
      head = prev;
    }
}

在main中,调用reverse(NULL,head);

答案 2 :(得分:3)

/* Reverses a linked list, returns head of reversed list
*/
NodePtr reverseList(NodePtr curr) {
    if (curr == NULL || curr->next == NULL) return curr; // empty or single element case

    NodePtr nextElement = curr->next;
    curr->next = NULL;
    NodePtr head = reverseList(nextElement);
    nextElement->next = curr;
    return head;
}

答案 3 :(得分:1)

另一种解决方案:

struct node *reverse_recur(struct node *temp)
{
    if(temp->link==NULL)
    {
        return temp;
    }

    struct node *temp1=temp->link;

    temp->link=NULL;

    return (reverse_recur(temp1)->link=temp);

}

答案 4 :(得分:0)

让链表成为 1→ 2 - > 3 - > 4

c中的函数是 -

struct linked_node * reverse_recursive(struct linked_node * head)
{
struct linked_node * first;/*stores the address of first node of the linked
list passed to function*/
struct linked_node * second;/* stores the address of second node of the
linked list passed to function*/
struct linked_node * rev_head;/*stores the address of last node of initial 
linked list. It also becomes the head of the reversed linked list.*/
//initalizing first and second
first=head;
second=head->next;
//if the linked list is empty then returns null
if(first=NULL)
   return(NULL);
/* if the linked list passed to function contains just 1 element, then pass
address of that element*/
if(second==NULL)
   return(first);
/*In the linked list passed to function, make the next of first element 
 NULL. It will eventually (after all the recursive calls ) make the
 next of first element of the initial linked list NULL.*/
first->next=NULL;
/* storing the address of the reverse head which will be passed to it by the
 condition if(second==NULL) hence it will store the address of last element
 when this statement is executed for the last time. Also here we assume that 
the reverse function will yield the reverse of the rest of the linked 
list.*/
rev_head=reverse(second);
/*making the rest of the linked list point to the first element. i.e. 
 reversing the list.*/
second->next=first;

/*returning the reverse head (address of last element of initial linked 
list) . This condition executes only if the initial list is 1- not empty 
2- contains more than one element. So it just relays the value of last 
element to higher recursive calls.  */
return(rev_head);
}

现在运行链接列表的功能1-> 2→ 3 - > 4

  • 内反转(& 1) 代码运行直到rev_head = reverse(& 2); //这里& 1是地址1.

功能列表为
   1(第一) - > 2(第二) - > 3 - > 4

  • 反向内部(& 2) 代码运行直到rev_head = reverse(& 3); 功能清单
    2(第一) - > 3(第二) - > 4

  • 反向内部(& 3) 代码运行直到rev_head = reverse(& 4); 如果功能列出清单 3(第一) - > 4(第二)

  • 反向内部(& 4) 终止条件second == NULL为true所以返回执行和 地址为4。

功能列表

4(第一) - > NULL(第二)

  • 回到倒退(& 3) 功能列表是
    NULL< -3(第一)4(第二)
    以及返回的rev_head =& 4的值

执行second-> next = first之后;  列表变为

NULL< - 3(first)< -4(second)

return(rev_head);由于rev_head =& 4

,执行传递& 4
  • 回到rev(& 2)
功能中的

列表是

NULL< -2(第一)3(第二)< -4

和rev_head是& 4,由rev(& 3)

返回

执行second-> next = first后,list变为

NULL< -2(第一)< -3(第二)< -4

返回(rev_head);执行时返回& 4到rev(& 1);

  • 回到rev(& 1)
功能中的

列表是

NULL< -1(第一)2(第二)< -3< -4

和rev_head的值是& 4,它通过reverse(& 3)

传递

现在第二个> next =第一个执行,列表变为

NULL< -1(first)< - 2(second)< -3< -4

返回(rev_head);执行// rev_head =& 4,由反向(& 2)返回 并且rev_head的值被传递给main函数。

希望这会有所帮助。我花了很多时间来理解这一点并写下这个答案。

答案 5 :(得分:0)

    To fix head also:

void reverse_list_recursive_internal (struct list **head, struct list *node)
{
    /* last node, fix the head */
    if (node->next == NULL) {
        *head = node;
        return; 
    }
    reverse_list_recursive_internal(head, node->next);
    node->next->next = node;
    node->next = NULL;
}

void reverse_list_recursive (struct list **head)
{
    if (*head == NULL) {
        return;
    }
    reverse_list_recursive_internal(head, *head);
}

答案 6 :(得分:0)

这是一种可以跟随递归反转SLL的漂亮方法:

1.    struct node* head; // global head
2.    void rec_reverse(struct node* prev, struct node* cur)
3.    {
4.        if (cur)
5.        {
6.            rec_reverse(cur, cur->next);
7.            cur->next = prev;
8.        }
9.        else
10.            head = prev;
11.    }

以这种方式调用函数:

rec_reverse(NULL, head);

方法:

  • 递归调用函数(第6行),我们转到最后一个节点 链表。
  • 然后我们用最后一个节点的地址更新head (第10行)。
  • 最后,我们将每个节点的链接指向其前一个节点(第7行)。

答案 7 :(得分:0)

在我看来,没有人提出过 tail-recursive 的算法。原则上,尾递归算法可以在没有堆栈的情况下编译(假设编译器足够智能),从而产生消耗更少内存的代码。

假设TList是单链表的自定义数据类型,它是指向作为link字段的结构的指针,用于访问列表中的下一个元素。

算法如下:

```

TList reverse_aux(TList l, TList solution) {
    if (l == NULL) {
        return solution;
    } else {
        TList tmp = l->link;
        l->link = solution;
        return reverse_aux(tmp, l);
    }
}

TList reverse(TList l) {
    return reverse_aux(l, NULL);
}

```

答案 8 :(得分:0)

ll *rev_list(ll *prev, ll *cur)
{
    if (!cur) {
        return prev;
    }

    ll *tmp = cur;
    cur = cur->next;
    tmp->next = prev;
    prev = tmp;
    return rev_list(prev, cur);
}

找到完整的代码:https://github.com/vijaythreadtemp/Data-Structures-And-Algorithms/blob/master/rev_link_list_rec.cxx