0

I wanna to write a function in C to reverse Linked list by recursion, however I see many ways as iterative way (which done with me) .. But in recursive way the function always return 0

Revers Linked List Code

node * r_reverseItem(node * head) {    
    if (head == NULL) //only one elem
        return NULL;

    node * currentNode = head;

    if(currentNode->next == NULL) { 
        //set HEAD to current TAIL since we are reversing list
        head = currentNode; 
        return head; //since this is the base case
    }

    r_reverseItem(currentNode->next);
    currentNode->next->next = currentNode;
    currentNode->next = NULL; //set "old" next pointer to NULL
}

So, what is the problem here in this code please? however, any useful examples or sites are also desirable.

EDIT

Just simply I know where is my fault. I forgot to add a return statement which (killer mistake). So the correct function as below:

   node * r_reverseItem(node * head) {    
    if (head == NULL) //only one elem
        return NULL;

    node * currentNode = head;

    if(currentNode->next == NULL) { 
        //set HEAD to current TAIL since we are reversing list
        head = currentNode; 
        return head; //since this is the base case
    }

       node * newNode =r_reverseItem(currentNode->next);
           currentNode->next->next = currentNode;
           currentNode->next = NULL; //set "old" next pointer to NULL
           return newNode;
}
8
  • 1
    Well, first this doesn't look right: if (head == NULL) //only one elem. If head is NULL, then you have zero elements. Commented Apr 29, 2014 at 12:15
  • what do you mean by that please? I sent a list with 1,2,3,4 items So it is not empty! I do not know where is the error here to print 0 Commented Apr 29, 2014 at 12:16
  • "recursive way the function always return 0" Sometimes it returns an undefined value, too, because return is not reached. Can you spot that place? Commented Apr 29, 2014 at 12:18
  • yes please, I am one who understand slowly, so what do you mean? I add return statement already in base case of recursion ?! Commented Apr 29, 2014 at 12:20
  • 1
    I suggest you return it from your function. Notice that your function has a return value that is not assigned to anything. Commented Apr 29, 2014 at 12:34

2 Answers 2

2

Original solution implemented in Java , i only translated it to C with your variable names

node* r_reverseItem(node * head)
{
    if( !head )   // empty list
        return NULL;

    if( !(head->next) ) //last element becomes the head of the reversed list
        return head;

    node *currentNode = head->next; //preserve next element
    head->next = NULL;

        node* newHead = r_reverseItem(currentNode);

    currentNode->next = head; 
    return newHead; //once you have head you don't change it 
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks really for your answer, BUT what does that line mean please return newHead; and for what we must use it? ok I have a big gab I must work on it to understand well the linked list! :(
you understand that you actually creating a new list, so you will have to return the head of this new list, right ? And as the list you trying to create is the reverse of the original the head of the new list is the tail of the original one. There is no use of it inside r_reverseItem, you only need it to have acces to the list after the function is done.
1

Check this web site , basically it explain three way of reverse a link list with example after you read them you will be able to made it recursively as you want

http://www.codeproject.com/Articles/27742/How-To-Reverse-a-Linked-List-Different-Ways

Comments

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.