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 9108925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:55:11+00:00 2026-06-17T02:55:11+00:00

I stumbled upon a weird problem with adding two linked lists into a third

  • 0

I stumbled upon a weird problem with adding two linked lists into a third one in Java, the first linked list “myList1”, the second linked list “myList2” and the third one “myList3”.

The combining method is supposed to to add the first LinkedList “myList1” then the second “myList2” into the third LinkedList “myList3”, but I faced a problem with adding them to third list while it’s empty, but if the third list has at least one element every thing goes smoothly.

The code:

Node current = myList1.head;            
while (current != null) {                                                                       
    Node newcurrent = myList3.head;
    int h1 = current.getData();                                  
    Node newNode = new Node(h1);
    if (newcurrent == null)     
        //the problem is with this code                                             
        newcurrent = newNode;                           
    else {                                     
        if (newcurrent.getLink() == null) {
            newNode.setLink(newcurrent.getLink());                                         
            newcurrent.setLink(newNode);                                      
        } else {                                        
            Node current11 = newcurrent;                                                
            while (current11.getLink() != null) {
                current11 = current11.getLink();
            }                       
            current11.setLink(newNode);                          
        }                                    
    }                                                           
    current = current.getLink();                            
}

The node is not added to the third LinkedList if the third list is empty, and I tried many other codes but it didn’t work either, but if I entered at least one element to the third LinkedList the list is added normally.

other codes I tried :

newcurrent.setLink(newNode);

and

newNode = newcurrent; 
newcurrent = newNode;

and

newNode.setLink(newcurrent); 
newcurrent.setLink(newNode);

and

newNode.link = newcurrent; 
newcurrent.link = newNode;
  • 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-17T02:55:12+00:00Added an answer on June 17, 2026 at 2:55 am

    You’re overcomplicating this a bit, I think. The links within the list are already there. You only need to link myList3.tail to myList1.head, no need to loop through adding each node independently. Since you don’t look like you are storing a tail, you’ll need to iterate to the end of myList3 to find it.

    if (myList3.head == null)
        myList3.head = myList1.head;
    else {
        Node list3iter = myList3.head;
        while (list3iter.getLink() != null) {
            list3iter = list3iter.getLink();
        }
        list3iter.setLink(myList1.head);
        }
    }
    

    One further note, I find it painful to try to keep track of names like current, current11, newcurrent, etc. They all mean just about the same thing to my brain. If you’re like me, a bit more descriptive naming might help you keep track of what your variables are meant to be doing here.

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

Sidebar

Related Questions

I've stumbled upon the following two weird looking properties: Process.GetCurrentProcess().MainModule; Assembly.GetExecutingAssembly().Location; These properties are
I stumbled upon a weird problem with Magento indexer. I tried to run it
Stumbled upon a weird Chrome-only rendering behavior for select-boxes: Select one option and during
First time I am writing some ASP.NET code and stumbled upon a weird bug.
I've stumbled upon a really weird situation with ASP.Net MVC, passing an id as
I stumbled upon this problem and it took my a while to realise what
I stumbled upon https://codereview.stackexchange.com/questions/10610/refactoring-javascript-into-pure-functions-to-make-code-more-readable-and-mainta and I don't understand the answer since the user uses
I stumbled upon a problem of how to make work together acts_as_taggable (on steroids)
I've stumbled upon the weird way (in my view) that Matlab is dealing with
I'm troubleshooting some weird behavior of a threaded application and have stumbled upon a

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.