gpt4 book ai didi

c++ - 在 O(1) 复杂度中连接 C++ 中的两个列表

转载 作者:行者123 更新时间:2023-11-27 22:34:54 25 4
gpt4 key购买 nike

如果我们知道最后一个元素,我们可以在 O(1) 时间内连接两个链表。那么,有什么方法可以使用内置数据结构在 C++ 中连接两个列表,或者我必须自己实现链表然后使用它?

最佳答案

std::list<int> l1 = create();
std::list<int> l2 = create();
l1.splice(l1.end(), l2);

请注意,这会清空 l2 并将其元素移动到 l1

关于c++ - 在 O(1) 复杂度中连接 C++ 中的两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55979935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com