Home > Back-end >  I am not getting how to concatenate two Linked List
I am not getting how to concatenate two Linked List

Time:10-03

I am not getting how to run the concatenate function using both Node and LinkedList as classes. If anyone knows how to do it, please let me know.

Here I have created two classes one for linked list and the former one for Node creation. Using create function and passing array as Linked list input. Also, I am getting the headref using getHead() and getHead2() functions which give the starting pointer of first and second Linked List respectively.

#include <iostream>
using namespace std;

class Node{
    public:
    int data;
    Node* next;
    
    Node(){
        data=0;
        next=NULL;
    }
    Node(int data){
        this->data=data;
        this->next=NULL;
    }
};

class LL{
    Node* first, *second;
    public:
    LL(){
        first=second=NULL;
    }
    
    void create(int arr[], int n){
        Node* t, *last;
        first=new Node();
        first->data= arr[0];
        first->next=NULL;
        last=first;
        
        for(int i=1;i<n;i  ){
            t=new Node();
            t->data=arr[i];
            t->next=NULL;
            last->next=t;
            last=t;
        }
    }

    void create2(int arr[], int n){
        Node* t, *last;
        second=new Node();
        second->data= arr[0];
        second->next=NULL;
        last=second;
        
        for(int i=1;i<n;i  ){
            t=new Node();
            t->data=arr[i];
            t->next=NULL;
            last->next=t;
            last=t;
        }
    }
    
    Node* getHead(){
        return first;
    }

    Node* getHead2(){
        return second;
    }
    
    void display(Node* p){
        while(p){
            cout<<p->data<<" ";
            p=p->next;
        }
        cout<<endl;
    } 
void concatLL(){
        Node* p=first;
        while(p->next){
            p=p->next;
        } 
        p->next=second;
        second=NULL;
    }

};

int main()
{
    LL l,l2;
    int arr[]={1,2,3,4,5,6,7};
    int arr2[]={2,5,7,8,9};
    int n=sizeof(arr)/sizeof(arr[0]);
    int n2=sizeof(arr2)/sizeof(arr2[0]);
    l.create(arr,n);
    l2.create2(arr2,n2);
    cout<<"Displaying first LL"<<endl;
    l.display(l.getHead());
    cout<<"Displaying second LL"<<endl;
    l2.display(l2.getHead2());

    cout<<"Displaying Linked list after concatination"<<endl;
    l.concatLL();
    l.display(l.getHead());
    return 0;
}

CodePudding user response:

p is equal to NULL when executing p->next=second; in concatLL() causing undefined behaviour since the while loop runs until p == NULL. You should use while(p->next) instead of while(p) and check that first is not a null pointer before.

CodePudding user response:

A linked list is still a Node, and as others said, at the end of your while loop p is pointed to NULL. You can't NULL->second=second neither NULL=second, so change your while loop stop condition:

void concatLL(){
        Node* p=first;
        while(p->next){
            p=p->next;
        } 
        p->next=second;
        second=NULL;
    }

};

Here is my solution:

#include <iostream>

using namespace std;

struct Node {
    Node* next;
    int value;

    Node(int value) {
        this->value = value;
        this->next = NULL;
    }

    Node(int n, int a[]) {
        value = a[0];
        next = NULL;
        Node* p = this;
        for (int i = 1; i < n; i  ) {
            p->next = new Node(a[i]);
            p = p->next;
        }
    }

    void concat(Node* head) {
        Node* p = this;
        while (p->next != NULL) {
            p = p->next;
        }
        p->next = head;
    }

    void display() {
        Node* current = this;
        while (current != nullptr) {
            cout << current->value << " ";
            current = current->next;
        }
        cout << endl;
    }
};

int main() {
    int a[] = { 1, 2, 3, 4, 5 };
    int b[] = { 6, 7, 8, 9, 10 };
    Node* list1 = new Node(5, a);
    Node* list2 = new Node(5, b);
    list1->display();
    list2->display();
    list1->concat(list2);
    list1->display();
    return 0;
}
  • Related