Help: reverse a linked list

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.
 
O

Obnoxious User

Fixed it myself, thanks.

Is there any way to delete your own post?

If you're religious, you could beg to your deity of choice.
The answer is no, your post is forever archived.
 
H

Hamster

Alf,

I missed some if/else structure when copying/pasting which left that
piece of code logically incomplete and confusing.

Thank you for your time.

OU,

Thanks to confirm...would be nice if there were such a feature, imo.
 
C

Chris M. Thomasson

Hamster said:
Hi,

Need help with my code on reversing a singly linked list, here's my
code:
________________________________________________________
node* reverse(node* list) {
node* head = NULL;
while (list) {
node* const next = list->next;
list->next = head;
head = list;
list = next;
}
return head;
}
________________________________________________________
 
P

Pascal J. Bourguignon

node* reverse1(node* list,node* result){
if(list){
node* next=list->next;
return(reverse1(next,(list->next=result,list)));
}else{
return(result);
}
}
node* nreverse(node* list){
return(reverse1(list,0));
}
// Notice that g++ -O3 implements TCO, so there's no recursive call here.


With better definitions it could be written as:

node* reverse1(node* list,node* result){
return(list?reverse1(rest(list),cons(first(list),result)):result); }
node* reverse(node* list){
return(reverse1(list,0)); }
// Still no recursive call, thanks to TCO.


that is:

struct node {
char* first;
node* rest;
node(char* aFirst,node* aRest):first(aFirst),rest(aRest){};
};

char* first(node* node){return(node->first);}
ndoe* rest(node* node){return(node->rest);}
node* cons(char* first,node* rest){return(new node(first,rest));}


________________________________________________________
node* reverse(node* list) {
node* head = NULL;
while (list) {
node* const next = list->next;
list->next = head;
head = list;
list = next;
}
return head;
}
________________________________________________________

This works ok, so what else do you want?
 
J

Jorgen Grahn

Fixed it myself, thanks.

Is there any way to delete your own post?

There used to be, but people started disabling it fifteen years ago
due to abuse. It's called /cancelling/ a posting.

In either case, it's more polite to show us your error and the
solution.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top