# How to reset a linked list i reversed

• 02-16-2010
Soulzityr
How to reset a linked list i reversed
I have a reverse method that messes with the original linked list inputted; however usually this is fixable by just reversing.

my problem is after i reverse it, how do i recover the original linked list?

Code:

```//compares p and q; if p is smaller, then return -1; if q is smaller, return 1 //return 0 if its exactly the same int compare(struct integer* p, struct integer* q) {   int count=1, count2=1;   struct integer *ptest=p, *qtest=q;   while(ptest->next!=NULL) {       ptest=ptest->next;       count++;              }   printf("count p= %d\n",count);   while(qtest->next!=NULL) {       qtest=qtest->next;       count2++;              }   //PTEST AND QTEST ARE CURRENTLY POINTING TO LAST DIGIT OF P AND Q   printf("count q= %d\n",count2);   if(count<count2) {       printf("p is less than q; returning -1\n");       return -1;   }   else if(count>count2) {       printf("q is less than p; returning 1\n");       return 1;    }   else {       printf("p=%d and q are equal length; no return yet\n",ptest->digit);       if(ptest->digit<qtest->digit) {         printf("returning -1\n");         return -1;                    }       else if(ptest->digit>qtest->digit) {         printf("returning 1\n");         return 1;        }       else {               }       return 0;   } } } //---------------PART OF ANOTHER FUNCTION; SEGMENT       p=reverse(p);       q=reverse(q);       printf("\nRevp: ");       print(p);       printf("-Revq: ");       print(q);       printf("\n");       while(p!=NULL && q!=NULL) {         if(p->digit<q->digit) {             printf("first else\n");             p=reverse(p);             q=reverse(q);             free(ptest);             free(qtest);             return -1;                  }         else if(p->digit>q->digit) {             printf("second else\n");             p=reverse(p);             q=reverse(q);             free(qtest);             free(ptest);             return 1;         }       p=p->next;       q=q->next;       }       return 0;```
• 02-16-2010
MK27
Well if your reverse function works properly, reverse it again.
• 02-16-2010
Soulzityr
ohh! maybe i should explain better;

i do reverse it again, however, due to the p=p->next; and q=q->next at end of while to compare, i lose the ones at the front of the number, makes sense???

the problem is i tried a struct integer* p=altp and substituted all the stuff to altp so i'd save the address of the first number, but it doesnt work =/ same output
• 02-16-2010
quzah
Then your reverse function doesn't work. If it did, you would simply call it over again:
Code:

```type *list; ... rev( &list ); /* or... */ list = rev( list ); ... /* later on... */ ... rev( &list ); /* or... */ list = rev( list );```
So, what's your reverse function look like, and how are you calling it?

Quzah.
• 02-16-2010
Soulzityr
my reverse function is up top...and it works just fine...just messes up the pointers up so it cant reclaim the previous order; i usually do just recall it twice; but the problem is i cant because the pointers move during the while loop, and when i have holder pointers, it screws up
• 02-16-2010
Soulzityr
my problem is that after i run these two methods, the number

6347 becomes 347 =[ i made a current pointer

struct integer* current=p; then did all my stuff with p, and then made p=current;, but its cut off, dishambled, many different kind of errors, hep lz?