链接列表的空间复杂性

发布于 2025-01-29 01:39:56 字数 276 浏览 5 评论 0原文

我的问题是,如果我们对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半边脸i 2025-02-05 01:39:56

空间复杂性保持线性或O(n)

The space complexity remains linear or O(n)

手心的海 2025-02-05 01:39:56

是的,是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.

笑着哭最痛 2025-02-05 01:39:56

我相信应该是o(1)空间复杂性。我们通常不会考虑空间复杂性中输入参数的大小,因为您实际上无能为力地改进或消除它们。我们只关心我们在算法中创建额外的缓冲区。考虑阵列相同。

  1. 没有其他缓冲区。空间复杂性:O(1)
public int[] noBufferMethod(int[] data) {
    int[] justReference = data; // Points to input so no extra space
    return justReference;
}
  1. 带有其他缓冲区。空间复杂性:O(n)
public int[] withBufferMethod(int[] data) {
    int[] buffer = new int[data.length]; // Creates additional buffer
    return buffer;
}

完全相同的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.

  1. No additional buffer. Space Complexity: O(1)
public int[] noBufferMethod(int[] data) {
    int[] justReference = data; // Points to input so no extra space
    return justReference;
}
  1. With additional buffer. Space Complexity: O(n)
public int[] withBufferMethod(int[] data) {
    int[] buffer = new int[data.length]; // Creates additional buffer
    return buffer;
}

The exact same applies to a LinkedList (or any collection).

潦草背影 2025-02-05 01:39:56

您要求其他空间复杂性,对吗?

在您的情况下,附加空间复杂性仍然是恒定的,只是创建一个参考变量并将其指向头部,不会再次创建整个链接列表,它只是指向头节点的另一个变量,并且没有创建任何额外的节点。任何新节点都是使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文