How can I resolve this code? please tell the mistake

It is showing False for every case

Hello @S19LPPP0192,

Finding/Detecting bugs in your code is an essential part of learning.
So, i would suggest you to give it try yourself first.

If you still can’t find the issue, let me know.

Suggestion:
Try to dry run your code on a small input. This would help you in understanding the flow of the program.
Example:
3
1 2 1

Hope, this would help.
If you face any issue, feel free to ask.

Not getting the mistake like for a string of 3 digits it is running fine

if you could tell the case I am missing I will try to change that

Hello @S19LPPP0192,

I think I should guide you here towards the solution.
Can you explain to yourself the following?

  1. What is the purpose of two cin statements inside the while loop of the bulidList() function?
    Don’t you think that the overwriting of data is taking place?
    Think… and correct.
    This is a silly mistake that you have done, which is natural.:wink:
    This was the reason, I wanted you to dry run your code.

  2. Once you would correct the above mistake, the actual error unwraps.
    This is Runtime error.
    Your Code is producing correct outputs for an odd length of the input list.
    And gives the error for the even length list.
    Example 1:
    3
    1 2 1
    Example 2:
    4
    1 2 2 1
    (Try both)

    Solution:
    Modify the following condition:
    if(p==NULL){}
    to
    if(p->next->next == NULL){}

  3. After this modification, you would get error for odd length list.
    Try the above examples again.
    Solution: Change the order.
    if(p->next == NULL)
    {
    second_head = q->next->next;
    break;
    }
    if(p->next->next == NULL)
    {
    second_head = q->next;
    break;
    }

Hope, his would help.
Give a like, if you are satisfied.

1 Like

Thank you very much for this detailed solution. Actually I did not checked my buildlist code during
my dry run !!!

Anytime.:wink:

BTW, I won’t mind a like.:joy: