广度优先搜索(BFS)使用队列(queue)实现。

1
2
3
4
5
6
7
8
9
priority_queue<
T, // 元素类型
Container, // 底层容器类型(默认是 std::vector<T>)
Compare // 比较函数类型
>
// 小根堆比较器实现优先队列
auto cmp = [](ListNode* a, ListNode* b) { return a->val > b->val; };
priority_queue<ListNode*, vector<ListNode*>, decltype(cmp)> pq(cmp);
// (cmp)传入比较规则

std::list 是双向链表容器。 std::list.splice(it_pos, list, it) 把一个从一个链表移动(不是复制!)到另一个链表(或同一个链表的不同位置),而且是 O(1) 时间复杂度。其中 it_pos 是指一个 list::iterator,表示插入点。