原题链接:https://leetcode-cn.com/problems/sort-list/

1、归并排序(递归版)

ListNode* sortList(ListNode* head) {if(head==NULL||head->next==NULL) return head;ListNode *p=head,*q=head->next;while(q!=NULL&&q->next!=NULL){//找出链表中点p=p->next;q=q->next->next;}ListNode *mid=p->next;p->next=NULL;ListNode *left=sortList(head);//将左右链表分别递归ListNode *right=sortList(mid);ListNode *dummy=new ListNode;ListNode *t=dummy;while(left!=NULL&&right!=NULL){//将左右两边链表排序if(left->val>right->val){t->next=right;right=right->next;}else{t->next=left;left=left->next;}t=t->next;}t->next=left!=NULL?left:right;return dummy->next;
}

2、归并排序(非递归版)

模拟归并排序的分隔部分,初始合并单元长度len=1,每一轮len=len*2,依次合并。

ListNode *sortList(ListNode* head){ListNode *p=head;int length=0;while(p!=NULL){p=p->next;length++;}ListNode *dummy=new ListNode;//哑头节点dummy->next=head;int len=1;while(len<length){ListNode *pre=dummy;//用于合并的辅助头部ListNode *h=dummy->next;while(h!=NULL){ListNode *h1=h;int i=len;while(h!=NULL&&i>0){h=h->next;i--;} if(i>0) break;//如果链表剩余个数少于len,则无需合并环节,直接break,执行下一轮合并ListNode *h2=h;i=len;while(h!=NULL&&i>0){h=h->next;i--;}int c1=len;int c2=len-i;//若h2存在,但以h2为头部的剩余个数少于len,也执行合并环节,h2单元长度为c2=len-iwhile(c1>0&&c2>0){if(h1->val<h2->val){pre->next=h1;h1=h1->next;c1=c1-1;}else{pre->next=h2;h2=h2->next;c2=c2-1;}pre=pre->next;}pre->next=c1>0?h1:h2;while(c1>0||c2>0){pre=pre->next;c1=c1-1;c2=c2-1;}pre->next=h;//合并完后,修改新的合并单元尾部pre指针指向下一个合并单元头部h}len=len*2;}return dummy->next;
}

leetcode算法题--排序链表★相关推荐

  1. leetcode算法题--相交链表

    原题链接: https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ https://leetcode-cn.com/pro ...

  2. leetcode算法题--分隔链表

    原题链接:https://leetcode-cn.com/problems/split-linked-list-in-parts/ vector<ListNode*> splitListT ...

  3. leetcode算法题--重排链表★

    原题链接:https://leetcode-cn.com/problems/reorder-list/ 1.map void reorderList(ListNode* head) {map<i ...

  4. leetcode算法题--环形链表 II★

    原题链接:https://leetcode-cn.com/problems/linked-list-cycle-ii/ 1.map ListNode *detectCycle(ListNode *he ...

  5. leetcode算法题--有序链表转换二叉搜索树★

    原题链接:https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/ 1.二分法+递归 TreeNode* ...

  6. leetcode算法题--反转链表 II★

    原题链接:https://leetcode-cn.com/problems/reverse-linked-list-ii/ 1.头插法 ListNode* reverseBetween(ListNod ...

  7. leetcode算法题--分隔链表★

    原题链接:https://leetcode-cn.com/problems/partition-list/ 1.双指针 ListNode* partition(ListNode* head, int ...

  8. leetcode算法题--旋转链表

    原题链接:https://leetcode-cn.com/problems/rotate-list/ 1.双指针法 相关题目:删除链表的倒数第N个节点 ListNode* rotateRight(Li ...

  9. leetcode算法题--删除链表的倒数第N个节点

    原题链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/ 双指针法 ListNode* removeNthFrom ...

最新文章

  1. MySQL 性能优化,优化设计及设计原则解读
  2. 嬴彻与东风商用车完成L3重卡A样车验收,加速自动驾驶产品化
  3. 安居客检测到网页抓取_安居客天津租房情况分析
  4. UI层调用WCF服务实例(源码)
  5. centos7查看当前端口_Centos7 防火墙开放端口,查看状态,查看开放端口
  6. 你们计算机专业的学生应该看看这篇文章
  7. 雅虎网站页面性能优化的34条黄金守则(转)
  8. 内是不是半包围结构_什么是自行车变速器上的“倾斜平行四边形”结构?
  9. vue.js 入门案例,双向绑定实现任务清单
  10. 网络协议分析-TCP协议分析
  11. PS学习笔记6-选区工具
  12. 苹果apple id无法申请开发者帐号问题
  13. dfuse 现在正式面向商业部署
  14. iframe异步加载技术及性能转
  15. django 中的聚合和分组 F查询 Q查询 事务cookies和sessions 066
  16. ncre计算机职业英语,NCRE计算机职业英语一级考试样卷.doc
  17. HBuilder X ——Uni app 学习笔记(一)
  18. vue3+jsQr实现手机浏览器调用本地摄像头扫描并识别二维码
  19. 彭于晏简单网页制作(HTML和css)
  20. 2023 小额现金贷网络贷款系统源码 支持打包成APP

热门文章

  1. python学费多少-Python开发学费一般多少钱?
  2. python图标-python实现的简版iconv
  3. python装饰器-理解Python装饰器(Decorator)
  4. python编程工具p-这里有123个黑客必备的Python工具!
  5. python语言编程流程图-用流程图带你更好理解python语法
  6. 记事本写python怎么运行-python入门之一个简单记事本
  7. codeforces D Good Substrings(hash)
  8. spring中resource设计与实现
  9. LeetCode Linked List Random Node(蓄水池采样算法)
  10. 计算字符串t在字符串s中出现的次数(KMP)