0

I am building a Linked list here.

public ListNode addNode(ListNode head, ListNode temp){
    if(head == null){
        head = temp;
    } else{
        ListNode curr = head;
        while(curr.next != null){
        curr = curr.next;
        }
     curr.next = temp;
    }
    return head;
}

I have to merge two Linkedlist here. For e.g. we assume head as 1->2->3->null, temp as 4->5->null, I need the output as 1->2->3->4->5->null

On the head list, we have to move to last node so that we can merge the temp list. To move the pointer to the last node of the head list, I am iterating till I get next element as null which means the curr list will have 3->null alone when the while loop exists. Then, I am adding the temp list as the next element of curr. So, I assume I get 3->4->5->null.

But, when I return head element, I am getting 1->2->3->4->5->null.

How this works> Both curr and head are pointing to same objects right? When we change curr, head should point to the same. How is this working?

Rafee
  • 3,975
  • 8
  • 58
  • 88
Prabhakaran
  • 177
  • 1
  • 1
  • 11
  • you are confusing the technical word `reference` with `mutability`, if the class design is self modifiable then chained object will change the original object state – Dickens A S Apr 05 '20 at 09:34
  • Can you explain what is happening here? – Prabhakaran Apr 05 '20 at 09:39
  • "When we change curr, head should point to the same." Why? – user207421 Apr 05 '20 at 09:59
  • @user207421 that's my question – Prabhakaran Apr 05 '20 at 10:10
  • @user207421 I don't find the answer from your link. If possible, read the post that you had shared and be specific before you mark it as duplicate – Prabhakaran Apr 05 '20 at 10:13
  • *"Both curr and head are pointing to same objects right?* - When you start the loop that is true. After you have gone round the loop once, it is no longer true. *"When we change curr, head should point to the same."* - No. See previous. *"How is this working?"* - It works because `curr` will point at the last node in the first list (not the first node) when you finally assign to `curr.next`. – Stephen C Apr 05 '20 at 10:55
  • 1
    Actually, the linked Q&A does answer your Question's title: "How object reference works in Java". I guess one lesson is to try to pick your question titles better. Another lesson is that if you are having difficulty visualizing what a graph algorithm is doing, try drawing the data structure on a piece of paper and hand-executing the code. – Stephen C Apr 05 '20 at 11:01
  • @StephenC Excellent. Thanks mate. One doubt, How this behavior changed after while loop? After iteration, the value of curr is different from head. As you said, curr is pointing to only the last element of head. Ideally speaking both should point to same object right? How does even happen internally? – Prabhakaran Apr 05 '20 at 11:06
  • 1
    After the loop the `curr` variable is no longer used. It doesn't matter what it points to. You are returning `head`. Don't confuse a `Node` with a variable that refers to a `Node`. – Stephen C Apr 05 '20 at 11:07
  • @StephenC right. I didn't choose the title appropriately because of which it is marked duplicate (without even reading the question) lol. But, I don't find my answer in the linked post. – Prabhakaran Apr 05 '20 at 11:08
  • 1
    The answer to your Question's title is in the linked Q&A :-) user207421 has interpreted your question as two separate questions, and found a duplink for one of them. Not unreasonable in the circumstances (IMO). – Stephen C Apr 05 '20 at 11:09
  • @StephenC Yeah I am returning head. My doubt is, value of `curr` is changed. I was under the assumption that the object it is pointing to is also changed. That's where I lack – Prabhakaran Apr 05 '20 at 11:10
  • 1
    I don't know why you would be assuming that. Please explain the reason for that assumption. Or ... perhaps it is time to get out that pencil and paper: see above. (PS - I have added another duplink that may help with your problem understanding this.) – Stephen C Apr 05 '20 at 11:12
  • In Java, If we assign `object a` to `object b`, ideally `object b` also points to object a. I change the property of object b and If I retrieve the `object a`, won't the `object a` have the latest value? (I am doing everything in a same method, it's not like passing to another method and manipulating values) – Prabhakaran Apr 05 '20 at 11:15
  • You are not assigning object `a` to an object `b`. You do not assign objects in Java. You assign **references**. So `curr = curr.next;` assigns the **reference** in `curr.next` to the **reference variable** `curr`. Time to read that 2nd duplink! – Stephen C Apr 05 '20 at 11:19
  • Perfecto. I understood it clearly now. Thanks a lot mate. You made my day. I understood it wrongly and confused throughout the day. I sent a twitter request to you :) – Prabhakaran Apr 05 '20 at 11:21
  • Just so you know, I don't respond to Twitter requests. That's why I removed by Twitter handle from StackOverflow. – Stephen C Apr 05 '20 at 11:22
  • oh ok. Thanks anyway. – Prabhakaran Apr 05 '20 at 11:23
  • "I was under the assumption that the object it is pointing to is also changed.". And that's exactly why this is a duplicate. That could only be true if Java supported pass-by-reference, and it doesn't. – user207421 Apr 06 '20 at 08:10

0 Answers0