Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8322489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:24:22+00:00 2026-06-08T23:24:22+00:00

copyRev void copyRev(node *head1, node *&head2) { stack<int> dat; node* curr = head1; while(curr!=NULL){

  • 0

copyRev

void copyRev(node *head1, node *&head2)
{

    stack<int> dat;
    node* curr = head1;
    while(curr!=NULL){
        dat.push(curr->data);
        curr = curr->next;
    }
    while(!dat.empty()){
        append(head2,dat.top());
        dat.pop();
    }

}

append

void append(node* &head, int data){

    if(head==NULL){
        head= new node;
        head->data = data;
        head->next = NULL;
    }
    else{

        node *curr = head;
        while((curr)->next!=NULL){
            (curr) = (curr)->next;
        }
        (curr)->next = new node;
        (curr) = (curr)->next;
        (curr)->data = data;
        (curr)->next = NULL;
    }
}

Well, I’m trying to clone the reverse of a linked list. I know how to reverse a list in place. But this code gives me a Bus error(code dumped).

This is how I call the function in main()

node *head;

    for(int i = 0;i<9;i++){
        append(head,i*i);
    }
    node *revHead;
    printList(head);

    copyRev(head,revHead);
    printList(revHead);

in append() function, I check for NULL head and create a new node if it is NULL. moreover, the append function properly adds the elements to the list. The problem arose only after I called the copyRev procedure.

I have tried in-place reversing and it works. I need to clone the reverse of a linked list. Both Iterative and Recursive solutions are welcome. Also please point out the error in the above code.

This is my own practice problem but not a homework problem

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:24:25+00:00Added an answer on June 8, 2026 at 11:24 pm

    You are not initializing your pointers:

    node *head;
    node *revHead;
    

    As a result they have indeterminate values (ie they are random).

    Using the values in these poitners in any way is undefined behavior. Which can be your error.

    Just initialize them:

    node*  head     = NULL;
    node*  revHead  = NULL;
    

    Recursive reverse of a linked list in place:

    void reverse(node*& head)
    {
         head = reverse(head, NULL);
    }
    node* reverse(node* item, node* next)
    {
         if (item == NULL)
         {    return next;
         }
         node*  iter = item->next;
         item->next  = next;
         return reverse(iter, item);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.