This has nothing to do with Java. In the first example you call lastNode
once and store the result in p
. In your second example, you call lastNode
twice, but after the first call, you add a new last node to your list, so the return value is different the second time!
0
solved Why can’t I use method return as a pointer in Java?