微软 SDE 真实面试题曝光:如何在 O(M+N) 时间、O(1) 空间内合并升序与降序单链表?

28次阅读
没有评论

There are 2 single linked list, one is ascending sorted, the other is descending sorted, write code to merge them together, result in ascending sorted.

For example:
L1: 1->2->3
L2: 6->5->4

Result:
1->2->3->4->5->6

Requirements:
Time complexity: O(M + N), M is the length of L1, and N is the length of L2
Space complexity: O(1)

Please write code to solve the problem, please define data structure that need to be used to solve the problem.


这题要求把一个升序单链表和一个降序单链表合并成整体升序,并且时间复杂度 O(M+N)、空间 O(1)。典型思路是先把降序链表原地反转成升序,然后像“合并两个有序链表”一样依次串接节点,考察链表操作细节与复杂度意识。

VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。

正文完
 0