1

I am studying the linked lists,and I'm having a little problem understanding references and pointers in Java (and also have some questions for which I can't find answer on the internet.) Namely,I have a class LinkedList that uses class Node like this:

public class LinkedList { public Node head; public LinkedList(int data) { head = new Node(data); } } 

and this is my Node class:

public class Node { public int data; public Node next; public Node(int data2) { data = data2; } } 

I also have method toStringLL() inside LinkedList which looks like:

public void toStringLL() { LinkedList l=this; while(l.head.next != null) { System.out.print(l.head.data+"->"); l.head = l.head.next; } System.out.print(l.head.data); System.out.println(""); } 

I don't understand why this toStringLL() changes my head of linked list,when I am iterating through it with l.head=l.head.next? Shouldn't my head stay the same?I thought that when I write LinkedList l=this,I can freely iterate through l,without affecting this(or am I wrong?) Now when I use Node n=this.head instead of LinkedList l=this,it works,but I have difficulty figuring out why that works and the previous doesn't.. Can someone explain to me the difference between these two?

3
  • 1
    Java has no pointers. Commented Oct 26, 2017 at 12:54
  • 3
    When you do LinkedList l = this; you are not making a copy of the list, you are just creating another variable that refers to the same LinkedList object. Variables in Java (of non-primitive types) are references, not objects themselves. Commented Oct 26, 2017 at 12:54
  • Please provide your code with Node n=this.head Commented Oct 26, 2017 at 13:01

2 Answers 2

5

I don't understand why this toStringLL() changes my head of linked list,when I am iterating through it with l.head=l.head.next?Shouldn't my head stay the same?

When you make the assignment

LinkedList l = this; 

you are not creating a new LinkedList object. You are just defining a references to the existing LinkedList object.

Therefore, l.head = l.head.next does exactly the same as this.head = this.head.next. Both change your original list.

On the other hand, when you write

Node n = this.head 

you are not changing the head of your List. You are declaring a variable that initially refers to the head of your List, but when you change that variable to refer to other Nodes of your list, the head variable of your list (this.head) remains unchanged.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it clear for you how OP iterates through the list, using Node n = this.head? I mean he says it works. But differenrce depends on how he uses it further.
@Russiancold I assumed OP uses n = n.next; to iterate over the nodes when using Node n = this.head
0

When you set

l.head=l.head.next; 

you change your head.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.