CAG
BAN USERI think it might not be a good idea to assume anything regarding lengths of two lists. Reversing the lists and adding members seemed easy to me on paper but when I thought about it I realized taking care of the "carry-over sum" might be a hassle, how did you handle it?
I think we can traverse both lists, separately, from front to end and recreate the number stored in each of them and add the two numbers to get the results. In order to recreate the number:
sum = 0;
node* current = head;
while(current)
{
sum = sum*10 + current->data;
current = current->next;
}
However one thing to consider in this approach is how big of a number may a list member may contain.
void RemoveSpaces(char* str)
{
if(!str) return;
int read = 0, write = 0;
//find first space character
while(str[write] != ' ')
{
if(str[++write]) continue; //there is a space
return; //there are no more space characters
}
read = write; //there were no spaces so far so read here onwards
do{
//find first non-space character
while(str[read] == ' ')
{
if(str[++read]) continue; //there is a non-space character
str[write] = '\0';
std::cout << "\n Modified String is: " << str << "\n";
return; //there are no more non-space character
}
str[write++] = str[read];
str[read++] = ' ';
//find first space character
while(str[write] != ' ')
{
if(str[++write]) continue; //there is a space
return; //there are no more space characters
}
}while(str[read]);
str[write] = '\0';
std::cout << "\n Modified String is: " << str << "\n";
}
You are right Upt, dont know what I was thinking. In that case the solution seems to work.
- CAG June 21, 2011