链接列表的空间复杂性
我的问题是,如果我们对Java或Python中的对象创建多个参考变量,那么空间复杂性会增加还是相同?
例如,我有一个链接的输入大小n的链接列表,该列表是头 如果我创建另一个参考变量重复,并将其指向头部,就像
public LinkedList duplicate(LinkedList head)
{
LinkedList duplicate=head;
return duplicate;
}
在这种情况下是o(1)或o(n)的其他空间复杂性是什么?
My question is that if we create multiple reference variables to an object in java or python, does the space complexity increases or it is same?
for example I have a linked list of input size n which is head
if i create another reference variable duplicate and point it to head like
public LinkedList duplicate(LinkedList head)
{
LinkedList duplicate=head;
return duplicate;
}
what is the additional space complexity is it O(1) or O(n) in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
空间复杂性保持线性或O(n)
The space complexity remains linear or O(n)
是的,是o(n)。新对象是在堆空间中创建的,并且对这些对象的引用存储在堆栈内存中。
Yes, it's O(n). New objects are created in heap space, and the references to these objects are stored in stack memory.
我相信应该是
o(1)
空间复杂性。我们通常不会考虑空间复杂性中输入参数的大小,因为您实际上无能为力地改进或消除它们。我们只关心我们在算法中创建额外的缓冲区。考虑阵列相同。完全相同的LinkedList(或任何集合)。
I believe it should be
O(1)
space complexity. We generally don't factor in the size of input parameters in space complexity because there's really nothing you can do to improve or eliminate them. We only care if we're creating an additional buffer in our algorithm. Consider the same with arrays.The exact same applies to a LinkedList (or any collection).
您要求其他空间复杂性,对吗?
在您的情况下,附加空间复杂性仍然是恒定的,只是创建一个参考变量并将其指向头部,不会再次创建整个链接列表,它只是指向头节点的另一个变量,并且没有创建任何额外的节点。任何新节点都是使用java中的
new
关键字创建的。总空间复杂性仍然是O(n),因为它是您的linkedlist占用的空间。
You are asking for additional space Complexity, right ?
Additional Space Complexity remains constant in your case, just creating a reference variable and making it point to the head, doesn't create the whole linked list again, it is simply another variable pointing to head node and it is not creating any extra nodes. Any new node is created using the
new
Keyword in java.total space complexity remains O(n) as it is space taken by your Linkedlist.