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

The Archive Base Latest Questions

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

I am new to java. The question might not be that clear. Let me

  • 0

I am new to java.
The question might not be that clear. Let me explain using code.
The code below is a function that prints out values in a linkedlist.
The first line of the function creates a reference to point to the same object that is pointed by the HeadNode reference. So any changes to currentNode will affect the object that HeadNode is pointing to.
Now, inside the while loop, I am making the change of the currentNode, but I observe the linkedList didn’t change after I exit the function. Why?

static void PrintLinkedList(ListNode HeadNode)
    {
        ListNode currentNode = HeadNode;
        while(currentNode != null)
        {
            System.out.println(currentNode.getData());
            currentNode = currentNode.getNext();
        }
    }

UPDATE:

the reason of me bring this quesiton up is that when I implemented a function to reverse a linkedlist, my tempNode become null after the following code executed:

static ListNode ReverseLinkedList(ListNode headNode)
    {
        ListNode headNodeTemp = headNode;
        headNodeTemp.setNext(null);
        ListNode tempNode = headNode.getNext(); //temp becomes null because headNode is changed. Why??
        ListNode currentNode = headNodeTemp;
        while(tempNode != 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-15T15:24:19+00:00Added an answer on June 15, 2026 at 3:24 pm

    So any changes to currentNode will affect the object that HeadNode is pointing to.

    No, currentNode is a reference to the object HeadNode also refers to. You never change that object, all you change is what currentNode refers to.

    currentNode = currentNode.getNext();
    

    makes currentNode point to the next object, it doesn’t change any object.

    To change the object, you need to call a method or set a property,

    currentNode.mutate();
    currentNode.property = 17;
    

    would change the pointed-to object, and these changes would be visible when the object is queried through HeadNode.

    currentNode = currentNode.getNext();
    

    just makes currentNode point to another object.

    In

    static ListNode ReverseLinkedList(ListNode headNode)
    {
        ListNode headNodeTemp = headNode;
        headNodeTemp.setNext(null);
    

    here, we call a method that changes the pointed-to object. Going by the name, it sets the next field to null. And the object headNodeTemp points to is the object headNode points to.

        ListNode tempNode = headNode.getNext(); //temp becomes null because headNode is changed. Why??
        ListNode currentNode = headNodeTemp;
        while(tempNode != null)
        {...
    

    You should move the

    ListNode tempNode = headNode.getNext();
    

    line above the setNext(null) line.

    static ListNode ReverseLinkedList(ListNode headNode)
    {
        ListNode headNodeTemp = headNode;
        ListNode tempNode = headNode.getNext();
        headNodeTemp.setNext(null);
        ListNode currentNode = headNodeTemp;
        while(tempNode != null)
        {...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Java/Eclipse/Android, so this is probably an easy (if not stupid) question:
I am relatively new to Java and while trying out some code came across
Well, that might be a strange question, and maybe just because I'm not familiar
I have a Java command that looks something like below: Foo f = new
Sorry, I'm new to Java, so this question might be unclear. I have been
I'm very new to Java, and especially the topics that I'm discussing below, but
I'm new to Java and I have a beginner question: NumberFormat is an abstract
Hi I am completely new to Java, so sorry if my question may sound
Forgive me, I'm new to Java and have an extremely basic question. I have
First of all, I'm fairly new to Java, so sorry if this question 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.