Insertion Sort List
2016-07-12 16:27
344 查看
对链表进行插入排序,比对数组排序麻烦一点。
View Code
ListNode *insertSortList(ListNode *head) { ListNode dummy(-1); for (ListNode *cur = head; cur != nullptr;) { //将当前结点插入到此结点之后 auto Pos = findPos(&dummy, cur->val); //保存当前结点的下一个结点 ListNode *temp = cur->next; //插入 cur->next = Pos->next; Pos->next = cur; //继续下一个结点 cur = temp; } } ListNode *findPos(ListNode *head, int val) { ListNode *pre = nullptr; for (ListNode *cur = head; cur != nullptr&&cur->val <= val; pre = cur, cur = cur->next); return pre; }
View Code
相关文章推荐
- golang并发编程的两种限速方法
- Swift - 侧滑菜单的实现(样例2:仿QQ,菜单带缩放效果)
- POJ 1386 有向图的欧拉路径
- [iOS item-services分发] 企业级账户分发-个人练手记录
- JPA学习笔记(14)——查询缓存
- Android笔记--简单的自定义View之组合模式
- 关于bootstrap 弹窗——点击空白处禁止关闭弹窗
- 连载:面向对象葵花宝典:思想、技巧与实践(3) - 面向过程 vs 面向对象
- 332. Reconstruct Itinerary
- easyui datagrid combox 与 bootstrap 共用时,bootstrap.min.js 出现JS异常
- 【NOIP2016模拟7.12】游戏
- JPA学习笔记(13)——JPQL
- Log4j.properties配置详解
- 替换空格
- 字符串运用-密码截取,字符串中的最长的回文串
- Spring Batch
- linux脚本后台运行
- The project: project which is referenced by the classpath, does not exist.
- Myeclipse Templates详解(一) —— Java模板基础
- 从艾优尼之死,看Freeme OS的深远价值