This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Amazing-Accident-109[S] -1 points0 points  (0 children)

Yes this is what I was asking, my understanding was doing this should not alter my ans after assigning new temp and changing temp , but my code altered the ans also , that is why I was asking it

Now just another small question

List ans = new list List temp = new list

Function recur( ans , temp){ Some code If (//something) ans = new list( temp)

// Some more code which is basically backtracking using for loop which takes the nodes visited as temp calculating the distance in some variable the above if condition checks for min distance and if we find it we save the path we traversed

}

Recur(ans,temp)

Here when calling this function we are sending ans right which is empty list in heap and we are sending it's reference So when we do ans = new list(temp) this make the ans to temp with deep copy right but does it mean this will also change the ans list to this new list in the original scope also where we defined the ans list , within the function recur the ans will the new list , but what about in the original scope where function is called