Create a reverse LinkedList in C++ from a given LinkedList

前端 未结 10 997
温柔的废话
温柔的废话 2020-11-30 08:37

I\'m having some trouble create a linkedlist in reverse order from a given linkedlist.

I come from a java background, and just started doing some C++.

Can yo

10条回答
  •  感动是毒
    2020-11-30 08:57

    This is done using just two temporary variables.

    Node* list::rev(Node *first)
    {
        Node *a = first, *b = first->next;
        while(a->next!=NULL)
        {
            b = a->next;
            a->next = a->next->next;
            b->next = first;
            first = b;
        }
        return first;
    }
    

    Also, you can do this using recursion.

提交回复
热议问题