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

  • SEARCH
  • Home
  • 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 5942105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:12:16+00:00 2026-05-22T16:12:16+00:00

I have the solution code here: // Pre-condition: The fronts of two linked lists

  • 0

I have the solution code here:

// Pre-condition: The fronts of two linked lists are provided.
// Post-condition: A linked list is returned that is the result of
// interleaving the elements from each provided list. 
// (e.g. {1, 2, 3} & { 4, 5, 6} would return {1, 4, 2, 5, 3, 6}

Node* interleave( Node*& front1, Node*& front2 ) {
    if( !front1 ) return front2;
    if( !front2 ) return front1;

    Node* third = front1->next; //this will become the third element
    Node* fourth = front2->next; // this will be come the fourth element

    front1->next = front2;
    front2->next = third;

    third = interleave(third, fourth);

    return front1;  
}

I kind of understand it but i would never be able to come up with something like this since i’m very bad at recursion. Is there another way to do this problem non-recursively? if so could you give me a hint? I tried this:

Node* interleave( Node*& front1, Node*& front2 ) {
    Node* newNode = new Node;
    while(front1!=NULL && front2!=NULL){
        newNode = front1->next;
        newNode = front2->next;
        front1 = front1->next;
        front2 = front2->next;
     }
    return newNode;
}

I’m sure it’s wrong but that’s the only thing i can come up with right now. Please help. Thank you

  • 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-05-22T16:12:17+00:00Added an answer on May 22, 2026 at 4:12 pm

    There are a few mistakes in your code:

    Node* interleave( Node*& front1, Node*& front2 )
    

    I don’t see the need for a reference to a pointer, since the first item in front1 will keep on being the first, and you don’t need to mess with front2 at all.

    Node* newNode = new Node;
    while(front1!=NULL && front2!=NULL){
        newNode = front1->next;
    

    This is causing a memory leak – you’re allocating at least sizeof(Node) bytes, but then you’re losing the reference to the pointer, and you won’t be able to delete it anymore.
    Also, you’re not doing anything with newNode, so you might throw it away as well.

    front1 = front1->next;
    front2 = front2->next;
    

    Basically you’re telling that front1 will rather point to the next element, and since you passed a reference to front1, you’re altering the real pointer. Eventually, front1 or front2 will be NULL and the loop will terminate, so at least one of the two given parameters will become useless. You’re never changing next, so the order will be left unchanged – you’re just walking through the lists.

    One approach could be to set front2’s value to front1->next, then swap pointers and iterate again:

    Node *a = front1, *b = front2;
    while (a && b) {
        Node* tmp = a->next;
        a->next = b;
        b = tmp;
        a = a->next;
    }
    return front1;
    

    I didn’t test this, but it should be close to working. You can replace the verbose swap code with std::swap() if you’re using stl.
    The idea is simple: suppose you have two lists:

    A -> B -> C -> NULL
    D -> E -> F -> NULL

    you say that A’s next item is going to be the first element in the second list, so D:

    A -> D -> E -> F -> NULL

    and then the second lists becomes the ancient A’s successor, so just B -> C -> NULL. You then advance a to point to its new next, or D, so you now have:

    D -> E -> F -> NULL
    B -> C -> NULL

    and you repeat:

    D -> B -> C -> NULL
    E -> F -> NULL

    B -> C -> NULL
    F -> NULL

    and so on until a NULL is met. Then front1, that still points to A, should have the right sequence (that is, unless I’m terribly wrong :p )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a solution that is missing a lot of code coverage. I need
I have a solution with two projects. One for project for the production code
I've tried two different methods of reusing code. I have a solution full of
I have created a new solution with a minimal amount of code that represents
I have no idea why is that. Here is my code and it works
Have you guys had any experiences (positive or negative) by placing your source code/solution
I have the following Solution: SomeProject.Ria (non Silverlight code) SomeProject.Ria.Silverlight (Silverlight light code, namespace
Does anyone have a good solution for integrating some C# code into a java
I have the following code in a full .NET framework solution: public delegate int
my solution has multiple projects and in one of them I have the code

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.