puzzling linked lists

here is a problem given to me by one of my friends.i have racked my brains but am unable to solve it.
Given a pointer to the head of a singly linked list, reverse the order of the list and return a pointer to the new head?
please provide me the solution ASAP i am very inquisitive

//Here is the stuff
ll=NULL;
if (l!=NULL) lll=l->next;
while (l!=NULL)
{
l->next=ll;
ll=l;
l=lll;
if (lll!=NULL) lll=l->next;
}
l=ll;
//End of the stuff

print(l);
}

The whole thing stand on the use of 3 pointers :
the main pointer that follow the list
a pointer to the previous element
a pointer to the next element;

Given that, the problem is very simple.

0

sanjiv040897Commented: 1997-04-08

To latha : My code could in fact be more clear with some comment.

To sanjiv : Very funny.
You have missed the case of a empty list.
And also something worst : out of your code :
while (pCurrentNode != NULL)
{
...//The content doesn't matter
}
return pCurrentNode ;
}
I THINK pCurrentNode COULD BE EQUAL to NULL WHEN RETURNED.

In my code, change :
l to pCurrentNode
ll to pPrevNode
lll to pNextNode
My code has been compiled and tested.

0

aleroyCommented: 1997-04-09

Dear aleroy,

I completely agree with u about the problems u have pointed out. I did not compile and test the program, I don't have time to do that. My intention was to give a quick start to latha and not to spoon-feed the solution. I hope latha knows enough C programming to fix such problems.