m0_61574664 2021-12-29 00:13 采纳率: 87.1%
浏览 38
已结题

合并k个排序链表,力扣上原题,有一些问题

class Solution {
public:
ListNode* mergeTwoLists(ListNode *a, ListNode *b) {
if ((!a) || (!b)) return a ? a : b;
ListNode head, *tail = &head, *aPtr = a, *bPtr = b;
while (aPtr && bPtr) {
if (aPtr->val < bPtr->val) {
tail->next = aPtr; aPtr = aPtr->next;
} else {
tail->next = bPtr; bPtr = bPtr->next;
}
tail = tail->next;
}
tail->next = (aPtr ? aPtr : bPtr);
return head.next;
}

ListNode* merge(vector <ListNode*> &lists, int l, int r) {
    if (l == r) return lists[l];
    if (l > r) return nullptr;
    int mid = (l + r) >> 1;
    return mergeTwoLists(merge(lists, l, mid), merge(lists, mid + 1, r));
}

ListNode* mergeKLists(vector<ListNode*>& lists) {
    return merge(lists, 0, lists.size() - 1);
}

};
这是力扣的原题官方解答,分治合并,我有几个问题,来个认真负责任的大佬
问:if ((!a) || (!b)) return a ? a : b;这段代码有什么意义呢,为什么不写成if ((a) || (b))
问:tail->next = (aPtr ? aPtr : bPtr);这段代码的优先级是什么,为什么不写成(tail->next = aPtr) ? aPtr : bPtr;
问:int mid = (l + r) >> 1;这段代码什么意思,为什么不去写成int mid = (l + r) /2,希望能讲详细一点
问: ListNode head, *tail = &head,我为什么不能写成 ListNode &head, tail = head
问:ListNode
mergeTwoLists(ListNode *a, ListNode b)为什么不能写成ListNode mergeTwoLists(ListNode a, ListNode b)

来个负责任的大佬

  • 写回答

2条回答 默认 最新

  • togolife 2021-12-29 18:20
    关注

    问:if ((!a) || (!b)) return a ? a : b;这段代码有什么意义呢,为什么不写成if ((a) || (b))
    这个是如果合并两个链表中有一个指针为空,就没必要执行了,返回不为空的;如果都是空的,返回NULL。
    写成if ((a) || (b)) 与 if ((!a) || (!b)) 逻辑就不一样了。

    问:tail->next = (aPtr ? aPtr : bPtr);这段代码的优先级是什么,为什么不写成(tail->next = aPtr) ? aPtr : bPtr;
    给tail->next赋值,将剩余的未合并的链表添加到末尾。(aPtr ? aPtr : bPtr) : aPtr不为空,就是剩余aPtr;否则就是bPtr;
    写成(tail->next = aPtr) ? aPtr : bPtr; 是什么作用呢?

    问:int mid = (l + r) >> 1;这段代码什么意思,为什么不去写成int mid = (l + r) /2,希望能讲详细一点
    效果是一样的,右移的执行效率高一点

    问: ListNode head, *tail = &head,我为什么不能写成 ListNode &head, tail = head
    定义head以及tail指针。
    写成ListNode &head, tail = head; 这是定义引用而且没有赋值,再定义tail变量? 那想这么写是想表达什么呢?

    问:ListNode* mergeTwoLists(ListNode *a, ListNode *b)为什么不能写成ListNode mergeTwoLists(ListNode a, ListNode b)
    这里链表采用指针结构,修改指针的指向对象,从而达到合并效果。如果写成ListNode方式,那就势必要从指针转换为变量,增加无谓的转换,还有可能出错。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月6日
  • 已采纳回答 12月29日
  • 创建了问题 12月29日

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上