H
Hamster
Hi,
Need help with my code on reversing a singly linked list, here's my
code:
void reverseList ( List& listObj)
{
ListNode*pHeadnew=listObj.lastPtr, *pTailnew=listObj.firstPtr;
ListNode* p1=listObj.firstPtr, *p2=listObj.firstPtr->nextPtr, *p3=p2-
while (p3!=0)
{
p2->nextPtr=p1;
p1=p2;
p2=p3;
p3=p3->nextPtr;
}
listObj.firstPtr=pHeadnew;
listObj.lastPtr=pTailnew;
listObj.lastPtr->nextPtr=0;
}
My test code created a 5-node list, after calling this function
(declared as friend in class List), the resulting list has only 1 node
left.
May anyone help spot what is wrong?
Thanks.
Need help with my code on reversing a singly linked list, here's my
code:
void reverseList ( List& listObj)
{
ListNode*pHeadnew=listObj.lastPtr, *pTailnew=listObj.firstPtr;
ListNode* p1=listObj.firstPtr, *p2=listObj.firstPtr->nextPtr, *p3=p2-
nextPtr;
while (p3!=0)
{
p2->nextPtr=p1;
p1=p2;
p2=p3;
p3=p3->nextPtr;
}
listObj.firstPtr=pHeadnew;
listObj.lastPtr=pTailnew;
listObj.lastPtr->nextPtr=0;
}
My test code created a 5-node list, after calling this function
(declared as friend in class List), the resulting list has only 1 node
left.
May anyone help spot what is wrong?
Thanks.