Skip to content
Advertisement

Kotlin single equal sign “=” object assignment time complexity

What is the time complexity of object assignment using the equals sign in Kotlin (or Java)?

JavaScript

More specifically if I have my own object ListNode

JavaScript

What would be the copy time below?

JavaScript

I find myself often doing this assignment since ListNode passes as val, and I can’t do

JavaScript

Advertisement

Answer

The assignment

JavaScript

does not make a new ListNode object that contains the same properties as l1. It just creates a new variable called l1Copy and points it to the same memory location as l1, so it’s an O(1) operation.

In the following code

JavaScript

there’s only one object, ListNode(1), and three variables pointing to it: node and var1 in the global scope, and n and n1 in f‘s scope.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement