K reverse linked list

runtime error

@ayu2321, you are getting run time error because you are inserting incorrectly, as you have not correctly handled the case when head is NULL
it should be

if(head == NULL){
    node*n = new node(data);
    head=n;  // you did'nt change head value
    return;
} 

Also , your print function is wrong , it should be

void print(node*head,int n){
    for(int i=0;i<n;i++){
        cout<<head->data<<" ";
        head=head->next; // you forgot this
    }
}

In case of any doubt feel free to ask :slight_smile:
Mark your doubt as RESOLVED if you got the answer


still giving runtime error

@ayu2321, it is giving correct answer on hackerblocks https://hack.codingblocks.com/app/contests/718/1326/problem

can you tell me for which case this is giving runtime error , and where you are submitting this code

https://online.codingblocks.com/app/player/51008/content/110419/4753/code-challenge

@ayu2321, try to submit the code in the hackerblocks link i have shared ,tell me if it works or not also submit in normal mode instead of collaboration mode since collaboration mode, your code is correct by the way there might be some server side problem

it is working fine in hackerblocks

still it is giving error after switching to normal mode

there must be problem from server side , i will report

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.