Hey There, This is RER, I am new to coding and I was solving the flattening the linkedlist problem on the geeks for geeks, and I am getting weird output and I am unable to debug , so the Problem is
and I my code looks like this
and weirdly enough , when I printed the linked list , I am getting proper out put you can see the end while loop for printing the list
but when removed , I am getting this
I know solution will be something stupid thing I have done, but requesting anyone looking to please help....
I am unable to figure out ..
I am new to forums so anyone can openly criticize my way of asking , if you find it wrong..
Thanks in advance..
and I my code looks like this
Code:
Node *flatten(Node *root) {
// Your code here
Node* ret = root;
Node* start = root;
while(start!=NULL)
{
//cout << start->data << endl;
Node* temp = root->next;
Node* moving = root->bottom;
while(moving!= NULL && temp!= NULL && temp->data < moving->data)
{
if(root== NULL)
{
break;
}
if(temp== NULL)
{
break;
}
//cout << "moving " << root->data << endl;
//cout << "temp "<< temp->data << endl;
root = root->next;
temp = temp->next;
}
if(root && moving){
//cout << moving->data << "ins set" << endl;
//cout << "root next" << root->data << endl;
if(!temp)
{
//cout << "its null so setting null" << endl;
}
root->next = moving;
moving->next = temp;
}
root = start->next;
if(root!=NULL){
//cout << "root val" << root->data << endl;
}
start = start->next;
}
// while(ret)
// {
// cout << ret->data << endl;
// ret = ret->next;
// }
return ret;
}
and weirdly enough , when I printed the linked list , I am getting proper out put you can see the end while loop for printing the list
but when removed , I am getting this
I know solution will be something stupid thing I have done, but requesting anyone looking to please help....
I am unable to figure out ..
I am new to forums so anyone can openly criticize my way of asking , if you find it wrong..
Thanks in advance..