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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:23:12+00:00 2026-06-04T20:23:12+00:00

I am trying to figure what’s wrong with this LinkedList implementation. The result is

  • 0

I am trying to figure what’s wrong with this LinkedList implementation. The result is not what I expected, I thought it should be: 9 4 2 7 5. However, when I run it, only 5 was added. Could someone please explain why? Thanks a lot!

public class LinkedList {

    LinkedList next;
    int value;

    public LinkedList(int value) {
        this.value = value;
        next = null;
    }

    public void add(int n, LinkedList k) {  
        LinkedList node = new LinkedList(n);
        node.next = k;
        k = node;
    }
}


public class LinkedListDemo {

    public static void main(String[] args) {
        LinkedList l = new LinkedList(5);

        l.add(7,l);
        l.add(2,l);
        l.add(4,l);
        l.add(9,l); 

        while(l != null) {
            System.out.println(l.value);
            l = l.next;
        }
    }
}
  • 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-04T20:23:14+00:00Added an answer on June 4, 2026 at 8:23 pm

    You’re losing your changes to k, since Java is passing the reference to k by value. This means that the assignment k = node does not persist when the method exits.

    Check out this question for more info on the problem 🙂

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

Sidebar

Related Questions

Trying to figure out the best way to do this: Should I do something
Trying to figure out what I'm doing wrong when trying to put $(this) in
Trying to figure out the best way to do this (And without crossing any
Trying to figure this out... I'm trying to accessthe top level of the dom
i've been messing around trying figure this out myself but its taking a while..
trying to figure out the best ActiveRecord inheritance strategy for this particular problem: I
Trying to figure this concept out. If you console.log this.get(content) before and after the
Trying to figure out a way to throw out attributes in this data that
Trying to figure out the best way to accomplish this. I have inhereted a
Trying to figure out why this swf which is loaded via javascript which is

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.