Reputation: 129
In singly linked list, it is specified that
public class ListNode {
int val;
ListNode next;
ListNode(int x) {val = x;}
}
In the code, it says
ListNode result = new ListNode(0);
s = 7;
result.next = new ListNode(s);
result = result.next;
System.out.println(result.next);
I am not sure why I did not get what I want. First, when I return result.next
, I think the pointer should travel to the beginning of the LinkedList and give me 7
but actually it prints nothing. Second, what is the meaning ofresult = result.next
? Does it mean that the pointer moves to the next node? Third, I only want to put 7
in the ListNode, but if I do not put the sentence ListNode result = new ListNode(0);
the program will fail in compile.
Upvotes: 1
Views: 9773
Reputation: 3317
The reason you are getting this error is that result.next when you call System.out.println(result.next) is null. Lets walk through the code line by line:
ListNode result = new ListNode(0);
//--this creates a node result : [0] -> null
// ^result pointing to this node
s = 7;
result.next = new ListNode(s);
//--this sets result.next to a new node with value 7 : [0] -> [7] -> null
// ^result ^result.next
result = result.next;
//--this moves result over to '7' node : [0] -> [7] -> null
// ^result ^result.next
System.out.println(result.next);
//--from this: [0] -> [7] -> null
// ^result ^result.next
//we can see that at this point in time, result.next = null
To answer your questions regarding "what does node = node.next mean", it means that the reference will "slide" to whatever node was the next node. From the LinkedList visualization:
[0] -> [7] -> null
-^node ^node.next
after calling:
node = node.next;
[0] -> [7] -> null
--------^node ^node.next
(EDIT: Answering a question from comments: )
Iterating over a LinkedList is typically done as follows:
ListNode result = new ListNode(0);
result.next = new ListNode(7);
while(result != null){
System.out.println(result.val);
result = result.next;'
}
Upvotes: 4
Reputation: 25169
It's not really that obvious what you are trying to do.
Perhaps these comments will help:
// create a new ListNode object with content 0, and put a reference to it
// in 'result'. At this stage it's 'next' field is null.
ListNode result = new ListNode(0);
s = 7;
// create a new ListNode object with content 7 and a null 'next' field,
// and make the first ListNode object's 'next' field refer to it.
result.next = new ListNode(s);
// now lose the reference to the first object, and instead make 'result'
// point to the second object. At this stage its 'next' field is still null.
result = result.next;
// now print the contents of the 'next' field of the second object, which
// will be null.
System.out.println(result.next);
Re your first question, perhaps you meant to print result.val
at the end, which will indeed be 7. Printing an object reference is not going to give you the val
field of that object without further work (e.g. overriding toString()
)
Re your second question, per the comments above, yes, you are effectively moving one step along the linked list, so 'result' now points to the second ListNode
you allocated (contents 7
).
I don't understand your third question. You have allocated two ListNode
s. If you only wanted one, then do:
result = new ListNode(7);
Upvotes: 2