I'm trying to write a reverse linked list code
The input, output is all ok.
Something is wrong in this code but it seem logical to me so can someone point the errors in here.
void reverseList( TheList *&head)
{// Pb : Point back, Pre : Previous num
TheList *Cur, *Pb, *Pre;
Cur = head ->nextNum;
Pb = head;
Pre = head;
while ( Pb != NULL)
{
Pb = Cur;
Cur = Cur->nextNum;
Pb->nextNum = Pre;
if ( Pb == NULL)
{
head->nextNum=NULL;
head = Pre;
}
Pre = Pb;
}
Output(head);
}
Thanks ^^, sr for my bad english
at the top of the line
Pb = Cur;
so when Cur = NULL => Pb =>NULL
the if statment there is for when the point reach the end of the list it will move the head point to the end after reverse
I got it working so thank you so much
This is my code after
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void reverseList( TheList *&head)
{// Pb : Point back, Pre : Previous num
TheList *Cur, *Pb, *Pre;
Cur = head ->nextNum;
Pb = head;
Pre = head;
while ( Cur != NULL)
{
Pb = Cur;
Cur = Cur->nextNum;
Pb->nextNum = Pre;
Pre = Pb;
}
head->nextNum = NULL;
head = Pb;
Output(head);
}