My code showing segmentation fault

Your code is giving runtime error because your circular linked list is not being formed.
Say If INPUT IS 1 2 3 4 5 2 3 -1
form a linked list 1->2->3->4>5 then you see that 2 has already been visited , so connect the next pointer of 5 to 2. This way you have created the circular linked list . After that just detect the cycle, remove the cycle, print the new linked list.
Maintain a map or a visited array…whenever you see that a node is already visited, connect next pointer of previous node to that node.

1 2 3 4 5 6 2 3 -1 1–>2–>3–>4–>5–>2–>3–> Segmentation fault (core dumped)

sir this is the output that is being shown by my compiler

Your buildlist function is not forming a circular linked list.
If it would have formed a circular linked list, then without removing the loop(from where circular linked list begins, here it is 2), this while loop in the print function would never end. I have commented out your runner function to check the actual linked list formed after taking the inputs.

Maintain a map or a visited array…whenever you see that a node is already visited, connect next pointer of previous node to that node. This way you can form a circular linked list

sir i am unable to create circular linked list how can i make it?

sir ye code likha to he but wrong he


void buildCycleList(Node *&head)
{
    unordered_map<int, Node *> hash;
    int x;
    cin >> x;
    if (x == -1)
    {
        head = NULL;
        return;
    }
    head = new Node(x);
    hash[x] = head;
    Node *current = head;
    while (x != -1)
    {
        cin >> x;
        if (x == -1)
            break;
        if (hash.find(x) != hash.end())
        {
            current->next = hash[x];
            return;
        }
        Node *n = new Node(x);
        current->next = n;
        current = n;
        hash[x] = n;
    }
    current->next = NULL;
}

You can do something like this. Dry run and check

for this i have to first learn what is hash

int he above example of 1 2 3 4 5 2 3
we first have to connect 5 with 2 but then it will run into an infinite loop. so to cure it we have to point 3 to null right?

You have to make a circular loop,
Once you connect the next of 5 to 2, circular loop is formed. The input after that ie. 3 does not even matter .

pr sir jb me output print karunga…cout<<head;
to meri loop ending condition kya hogi in print function.
pehle hm likhte the while(temp!=null)…ab kya likhenge

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.