1

The void reve(struct Node *head) and display(struct Node *head) methods take one argument - the head of the linked list. I want to print the whole linked list but my display function print only 4.

#include <iostream>

using namespace std;

struct Node {
    int data;
    struct Node *link;
};

void display(struct Node *head) {
    if (head == NULL) {
        return;
    }
    cout << head->data << "\t";
    display(head->link);
    //head = head->link;
}

struct Node *reve(struct Node *head) {
    struct Node *p = head;
    if (p->link == NULL) {
        head = p;
        return head;
    }
    reve(p->link);
    struct Node *temp = p->link;
    temp->link = p;
    p->link = NULL;
}

struct Node *insert(struct Node *head, int new_data) { 
    Node *new_node = new Node(); 
    new_node->data = new_data; 
    new_node->link = head; 
    head = new_node; 
    return head;
}

int main() {
    Node *head = NULL;
    head = insert(head, 1);
    head = insert(head, 2);
    head = insert(head, 3);
    head = insert(head, 4);
    cout << "The linked list is: ";
    //display(head);
    head = reve(head);
    display(head);
    return 0;
}

Output enter image description here

4 Answers 4

1

If you want the recursive way:

Node* reverse(Node* head) 

{ 

    if (head == NULL || head->next == NULL) 

        return head; 



    /* reverse the rest list and put  

      the first element at the end */

    Node* rest = reverse(head->next); 

    head->next->next = head;

    head->next = NULL; 

    /* fix the head pointer */

    return rest; 

} 



/* Function to print linked list */

void print() 

{ 

    struct Node* temp = head; 

    while (temp != NULL) { 

        cout << temp->data << " "; 

        temp = temp->next; 

    } 

} 
Sign up to request clarification or add additional context in comments.

Comments

1

The function reve does not return a value if p->link is not NULL.

Since head has more than 1 element, head = reve(head); has undefined behavior.

Reversing a linked list is much easier to implemented in a simple loop than with recursion:

struct Node *reve(struct Node *p) {
    if (p != NULL) {
        struct Node *prev = NULL;
        while (p->link) {
            struct Node *next = p->link;
            p->link = prev;
            prev = p;
            p = next;
        }
    }
    return p;
}

If your task requires recursion, you can make a extract the first node, reverse the remainder of the list and append the first node. Beware that this is not tail recursion, hence any sufficiently long list may cause a stack overflow.

struct Node *reve(struct Node *head) {
    if (head != NULL && head->link != NULL) {
        struct Node *first = head;
        struct Node *second = head->link;
        head = reve(second);
        first->link = NULL;   // unlink the first node
        second->link = first; // append the first node
    }
    return head;
}

2 Comments

My task is reverse a linkled list using recursion. I know about the method using loop.
@MukulTarania: I added a recursive version. Your approach was missing some of the steps.
1

In C++ you need not to use keywords struct or class when an already declared structure or a class is used as a type specifier.

The function reve has undefined behavior.

First of all head can be equal to nullptr. In this case this statement

if (p->link == NULL) {

invokes undefined behavior.

Secondly the function returns nothing in the case when p->link is not equal to nullptr.

    //...
    reve(p->link);
    struct Node *temp = p->link;
    temp->link = p;
    p->link = NULL;
}

Here is a demonstrative program that shows how the functions can be implemented. I used your C approach of including keyword struct when the structure is used as a type specifier.

#include <iostream>

struct Node
{
    int data;
    struct Node *link;
};

struct Node * insert( struct Node *head, int data )
{
    return head = new Node{ data, head };
}

struct Node * reverse( struct Node *head )
{
    if ( head && head->link )
    {
        struct Node *tail = head;

        head = reverse( head->link );

        tail->link->link = tail;
        tail->link = nullptr;
    }

    return head;
}

std::ostream & display( struct Node *head, std::ostream &os = std::cout )
{
    if ( head )
    {
        os << head->data;
        if ( head->link )
        { 
            os << '\t';
            display( head->link, os );
        }
    }

    return os;
}

int main() 
{
    struct Node *head = nullptr;

    const int N = 10;

    for ( int i = 0; i < N; i++ )
    {
        head = insert( head, i );
    }   

    display( head ) << '\n';

    head = reverse( head );

    display( head ) << '\n';

    return 0;
}

The program output is

9   8   7   6   5   4   3   2   1   0
0   1   2   3   4   5   6   7   8   9

Comments

1

display is fine.

First thing I have notices is that you are trying to modify a copied value. For example, line 16. This code has no effect.

Note that you have a bug on insert: You return head instead of new_node.

Your version fails for lists with more than 1 item. reve() is supposed to return the last node of the original list, which you do not, hence lastNode would not point to the last node of the reversed list. So my advice is that you keep it aside.

So, reve:

struct Node* reve(struct Node* head) {
    if (head->link == NULL) {
        return head;
    }

    struct Node* lastNode = reve(head->link);
    lastNode->link = head;
    head->link = NULL;

    return head;
}

and main:

int main() {
    Node* head = NULL;
    Node* last_node = head = insert(head, 1);
    head = insert(head, 2);
    head = insert(head, 3);
    head = insert(head, 4);
    head = reve(head);
    cout << "The linked list is: ";
    // Now, last_node is the new head
    display(last_node);
    return 0;
}

1 Comment

Your version fails for lists with more than 1 item. reve() is supposed to return the last node of the original list, which you do not, hence lastNode would not point to the last node of the reversed list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.