reversing linked list

后端 未结 9 1133
星月不相逢
星月不相逢 2020-12-10 16:52

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_lis         


        
相关标签:
9条回答
  • 2020-12-10 17:06

    I don't see the benefit of recursion here, iteration will work just as well. It's been forever since I've written C (and no easy way to test the following for syntax errors... or cringe core dumps, but you get the idea).

    node *reversed_list(node *list) {
        node *fwd=list;//Purely for readability
        node *last=null;
        node *next=null;
        node *rev=null;
        do {
            //Cache next
            next=fwd->next;
            //Set current
            rev=fwd;
            //Reset next to point back
            rev->next=last;
            //Update last
            last=fwd;
            //Forward ptr;
            fwd=next;
        } while (fwd!=null);
        return rev;
    }
    

    Pretty sure your *list is useless after you've called this since it's now pointing to last element of the list which has ->next=null, could just update that instead of returning the pointer.

    Update (for recursive solution)

    As others have said, your new tail is messed up (points back at the last element, but should point to null)... and you don't return the correct head, you return the second element. Consider the list a->b->null with your algorithm:

    p=a,
    c=b;
    c=
       p=b
       c=null
       return b; //b->null
    c=b
    c->next=a //b->a
    return a; //a->b, b->a, a returned
    //But what you wanted is a->null, b->a, and b returned
    

    The following updated code will fix:

    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;
               parent->next=null; //Fix tail
               printf("\n %d  %d \n",current->value,parent->value);
               return current; //Fix head
           }
    
      }
    

    With list a->b->null:

    p=a,
    c=b;
    c=
       p=b
       c=null
       return b; //b->null
    c=b
    c->next=a //b->a
    p->next=null //a->null
    return b; // b->a->null
    
    0 讨论(0)
  • 2020-12-10 17:06

    Here goes recursive code to reverse a linked list.

    list * reverse(list * head)
    {
        if( head == NULL || head -> link == NULL )
            return head;
        list *node = reverse( head- > link );
        head -> link -> link = head;
        head -> link = NULL;
        return node;
    }
    
    0 讨论(0)
  • 2020-12-10 17:07

    Suppose I have a linked list:

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

    Your code converts it to:

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

    Notice that the first element still points back to 2.

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

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

    which is in fact the correct structure.

    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;
           }
    
      }
    
    0 讨论(0)
  • 2020-12-10 17:07

    You forgot to update the next member of the first item on the linked list. Add parent->next = NULL; before the recursion call.

    0 讨论(0)
  • 2020-12-10 17:13

    Severl versions above are not working as OP wanted, so here is my recursive version tested fine:

    node * reverseRecursive(node *p,node **head)
    {
         if(p->next == NULL)
         {
             *head = p;
              return p;
         }
         node *before;
         before = reverseRecursive(p->next,head);
         before->next = p;
         p->next = NULL;
         return p;
    }
    
    //call from main
    node*head; 
    //adding value
    //assuming now head is now 1->2->3->4->NULL
    node* newHead;
    reverseRecursive(head,&newHead);
    //now now newHead is now 4->3->2->1->NULL
    
    0 讨论(0)
  • 2020-12-10 17:21

    You need to set the new tail (i.e the old head)'s next pointer to NULL

    EDIT: Here's a recursive version

    node *reverse_list_recursive(node *list)
      {
          node *parent = list;
          node *child = list->next;
          node *new_head;
    
    
        if (child == NULL)
              return parent ;  /* new head */
    
        new_head = reverse_list_recursive(child)
        child->next = parent; /* Old parent is the new child of the old child when reversed */
        parent->next = NULL; /* might be tail, will be overwritten after we return if we're not at the top level */
        return new_head;
    }
    
    0 讨论(0)
提交回复
热议问题