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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:44:35+00:00 2026-06-17T15:44:35+00:00

I know the problem with my code is (should be) stupid. But would appreciate

  • 0

I know the problem with my code is (should be) stupid. But would appreciate all help.

enter image description here

public void transferFrom(LinkedIntList list2) {

   // Point to first node of list1
   ListNode current = front;

   // Move to the last node of list1
   while(current != null) {
      current = current.next;
   }

   // Last node of list1 -> firs node of list2
   current.next = list2;

   list2 = null;

}

Problem line is current.next = list2;. Data type mismatch because current.next is ListNode and list2 is LinkedIntList.

If I rather use current.next = list2;, I get NullPointerException for this line.

What should I be doing?

EDIT: Fixed!

public void transferFrom(LinkedIntList list2) {

   // Point to first node of list1
   ListNode current = front;

   // Move to the last node of list1
   while(current != null && current.next != null) {
      current = current.next;
   }

   // Last node of list1 -> first node of list2
    if(front == null) {
        front = list2.front;
    } else {
        current.next = list2.front;
   }

   list2.front = null;

}
  • 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-17T15:44:36+00:00Added an answer on June 17, 2026 at 3:44 pm

    It would help if you posted the class definitions for LinkedIntList and ListNode, and also told us what this method is actually supposed to be doing. But I’m assuming that a LilnkedIntList contains a ListNode front member and that you are trying to append the contents of list2 to this (which is another LinkedIntList). Your troublesome line should probably be:

    current.next = list2.front;
    

    However, you have another problem: your while loop is guaranteed to exit with current == null, which is not what you want at all. The loop condition should be (current.next != null), not (current != null).

    Finally, if you want to empty out list2, the way to do it is list2.front = null;. Assigning to list2 inside the method does nothing.

    Here’s a version of your code incorporating all of my suggestions:

    public void transferFrom(LinkedIntList list2) {
    
       if (front == null) {
           front = list2.front;
       } else {
           // Point to first node of list1
           ListNode current = front;
    
           // Move to the last node of list1
           while(current.next != null) {
              current = current.next;
           }
    
           // Last node of list1 -> firs node of list2
           current.next = list2;
       }
    
       // empty out list2
       list2.front = null;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve the problem here but I don't know why my code
I have a weird problem, dont know which code should I post because I
I don't know if the problem is in my PHP code or on the
I know what is the problem but I don't really understand why it is
i know it is so simple problem but eventually it isn't working and i
I don't know if I'm too stupid or something similar but I have a
I know this is very stupid question, but I wanted to clarify this. Let's
I'm facing a problem that I assumed would be very easy to solve but...
I've got a simple problem but I don't know why it doesn't work. I
I've the following simplified code which describes my problem: public interface IMyUser { int

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.