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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:56:02+00:00 2026-06-13T20:56:02+00:00

I have a linked list using objects of class ListNode ListNode has the following

  • 0

I have a linked list using objects of class “ListNode”

ListNode has the following non static methods:

getValue()
setValue(Object obj)
getNext()
setNext(ListNode ln)

It’s constructor takes a value and a next.

In my main method in my driver class, create my linked list:

ListNode head = new ListNode("Overflow!", null);
head = new ListNode("Stack", head);
head = new ListNode("in", head);
head = new ListNode("is", head);
head = new ListNode("This", head);

I have a method called printList(ListNode ln).

I call it twice consecutively in my main method like this:

printList(head);
System.out.println();
printList(head);

My method looks like this:

public static void printList(ListNode head)
{
    while(head != null)
    {
        System.out.print(head.getValue()+" ");
        head = head.getNext();
    }
}

In my method, the reference is changed to point to a different object each time in the while loop. So after I exit the method, the reference “head” should be pointing to a null, right? However, when the printList(head) is called the second time, it magically prints all the elements in the list!

Here is what the jGrasp console shows:

 ----jGRASP exec: java StackOverflowQuestionExampleClass

This is in Stack Overflow! 
This is in Stack Overflow! 
 ----jGRASP: operation complete.

Here is the listnode class my teacher told me to use:

//Thomas Bettge, TJHSST, 10-20-2006
    public class ListNode
   {
      private Object value;
      private ListNode next;
       public ListNode(Object v, ListNode n)
      {
         value=v;
         next=n;
      }
       public Object getValue()
      {
         return value;
      }
       public ListNode getNext()
      {
         return next;
      }
       public void setValue(Object newv)
      {
         value=newv;
      }
       public void setNext(ListNode newn)
      {
         next=newn;
      }
   }
  • 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-13T20:56:04+00:00Added an answer on June 13, 2026 at 8:56 pm

    Confusion is probably stemming from the fact that you have 2 labels with the same name “head”. The “head” argument in the printList method is a new reference to the object that was passed in. reassigning it doesn’t affect the target of the original reference (in the sense that t won’t cause it to reference something else. an aside: changes to the state of the referenced object will have an effect, as it’s the same object, regardless of what is referencing it).

    might make it clearer to look at your code like this:

    public void yourMainMethod() {
        ListNode head = new ListNode("Overflow!", null);
        head = new ListNode("Stack", head);
        head = new ListNode("in", head);
        head = new ListNode("is", head);
        head = new ListNode("This", head);
    
        printList(head);
        System.out.println();
        printList(head);
    }
    
    //note different name, to clarify this is a separate reference
    public static void printList(ListNode node) {
        while(node != null)
        {
            System.out.print(node.getValue()+" ");
            node = node.getNext();
    
            //node.setValue(new Object());//note that this would change the state inside the ListNode passed in
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function defined inside my linked list class. The declaration in
I have a recursive object, a linked list really: public class LinkedList { public
I have a vector of Linked list pointers. Each LinkedList has a head pointer
I have to implement a one linked list but it should put object in
I have a program that contains a doctor class, and each Doctor object has
I am implementing a sorted list using linked lists. My node class looks like
I have a Linked List and I want to implement a function: Random_Shuffle_List (struct
I have a linked list, which stores groups of settings for my application: typedef
Say you have a linked list structure in Java. It's made up of Nodes:
I have a singly-linked list containing 1 value in each element. struct listElement {

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.