Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm practicing with linked lists using C. I'm a beginner programmer just started learning C two weeks ago.

I understand the concept of linked list very well and I was trying to reverse a linked list using recursion.

I know how to reverse it using a while loop, however with the recursion I'm stuck on how to point the head pointer to the last number address instead of the initial first number address after I reverse the list.

Below is my reverse function. The head is defined in main and I'm calling it by reference into my function. I know I can solve the issue by calling it by value and just writing in main() head=reverse(node* head); or by defining head in the dynamic memory and just accessing it straight from the function. I don't want to do that. I want to call it by reference from main and the function to return void.

Here is my reverse function:
void reverse(struct node** head)
    struct node* p=*head;

    {  *head=p;



Say The list is: 5 6 7 2 3 4 The output I'm getting after reverse is: 5
Thank you!

What I have tried:

I know the reason is because the head is still pointing at the first number address. I cant figure it out how to point it to the last number address. It should have done that in the if statement when it breaks the recursion function. But its not working.
Updated 21-Sep-16 23:27pm

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: tale a sheet of paper and try to do it by hand, your program should use the same procedure.
Share this answer
First off ask yourself why you want the function to not return anything and why you want to modify the function argument. If you think you've got a good reason then have a look at functional programming and why functions in that never modify their arguments.

By giving yourself the requirements you have you're essentially trying to solve a problem in a deliberately obscure and impractical way. Just because C is great at manipulating addresses doesn't mean it's a good idea to mess around with raw memory when there's a cleaner way of doing it.
Share this answer
Member 12752823 22-Sep-16 12:17pm    
That's true, I should be efficient with my code. No need to make it impractical. Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900