Flattening linked list problem

Joined
Dec 3, 2024
Messages
3
Reaction score
0
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
forumFlatteninglinkedlist.jpeg


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

1733226820913.png



but when removed , I am getting this

1733226859598.png


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..
 
Joined
Sep 21, 2022
Messages
216
Reaction score
32
Your output uses the next pointer.

Their output routine uses the bottom pointer, as stated on page 1.

I have not digested your program yet, this is just my first impression.
 
Joined
Sep 21, 2022
Messages
216
Reaction score
32
Your solution is interesting, that would not have been my first approach.

I would have worked from right to left, merging 2 vertical lines at a time, like a zipper.
 
Joined
Dec 3, 2024
Messages
3
Reaction score
0
Your output uses the next pointer.

Their output routine uses the bottom pointer, as stated on page 1.

I have not digested your program yet, this is just my first impression.
Hii , Very Thank you for your solution , in the first minds, I would have done like storing the variables and then then sort them in the array then populate them as it is , but I want to kind a play with the linked lists , so I tried no memory approach..

your reply was helpfuil , knowing , I was trying to populate next instead of bottom , it was dumb of me...
Thanks for pointing that...

//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
struct Node *next;
struct Node *bottom;

Node(int x) {
data = x;
next = NULL;
bottom = NULL;
}
};

void printList(Node *Node) {
while (Node != NULL) {
printf("%d ", Node->data);
Node = Node->bottom;
}
printf("\n");
}


// } Driver Code Ends
/* Node structure used in the program

struct Node{
int data;
struct Node * next;
struct Node * bottom;

Node(int x){
data = x;
next = NULL;
bottom = NULL;
}

};
*/

class Solution {
public:
// Function which returns the root of the flattened linked list.
Node *flatten(Node *root) {
Node* l1 = root;
Node* l2;
Node* l3;
Node* totalStart = l1;

if(root->next)
{
l2 = root->next;
}
else
{
l2 = NULL;
return root;
}
if(root->next && root->next->next)
{
l3 = root->next->next;
}
else{
l3 = NULL;
}
l1->next = NULL;
Node* temp;
//cout << l1->data << endl;
while(l1!=NULL && l2!=NULL){
totalStart = l1;
if(l1->data > l2->data)
{
temp = l2;
l2 = l1;
l1 = temp;
totalStart = l1;
}
l1->next = NULL;
while(l1!= NULL && l2!= NULL)
{
//Node* temp2;
Node* back = l1;
l1 = l1->bottom;
while(l1!= NULL && l2!= NULL && l1->data <= l2->data)
{
//cout << l1->data << endl;
back = back->bottom;
l1 = l1->bottom;
}
back->bottom = l2;
temp = l1;
l1 = l2;
l2 = temp;
// back->next = l2;
// temp = l1;
// l1 = l2;
// l2 = temp;

}
l1 = totalStart;
l2 = l3;
if(l3){
l3 = l3->next;
}
}
return totalStart;
}
};


//{ Driver Code Starts.

int main() {
int t;
cin >> t;
while (t--) {
int n;
vector<int> work;
string input;
getline(cin, input); // Read the entire line for the array elements
getline(cin, input); // Read the entire line for the array elements
stringstream ss(input);
int number;
while (ss >> number) {
work.push_back(number);
}
n = work.size();

Node *head = NULL;
Node *pre = NULL;

for (int i = 0; i < n; i++) {
int m = work - 1;
int data;
cin >> data;
Node *temp = new Node(data);
if (head == NULL) {
head = temp;
pre = temp;
} else {
pre->next = temp;
pre = temp;
}

Node *preB = temp;
for (int j = 0; j < m; j++) {
int temp_data;
cin >> temp_data;
Node *tempB = new Node(temp_data);
preB->bottom = tempB;
preB = tempB;
}
}

Solution ob;
Node *root = ob.flatten(head);
printList(root);
}
return 0;
}

// } Driver Code Ends
 

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
474,170
Messages
2,570,925
Members
47,466
Latest member
DrusillaYa

Latest Threads

Top