147. Insertion Sort List
来源:互联网 发布:java招聘系统源码 编辑:程序博客网 时间:2024/06/08 04:15
Sort a linked list using insertion sort.
画个图一目了然
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* insertionSortList(ListNode* head) { ListNode* new_head = new ListNode(0); new_head -> next = head; ListNode* pre = new_head; ListNode* cur = head; while (cur) { if (cur -> next && cur -> next -> val < cur -> val) { while (pre -> next && pre -> next -> val < cur -> next -> val) pre = pre -> next; /* Insert cur -> next after pre.*/ ListNode* temp = pre -> next; pre -> next = cur -> next; cur -> next = cur -> next -> next; pre -> next -> next = temp; /* Move pre back to new_head. */ pre = new_head; } else cur = cur -> next; } ListNode* res = new_head -> next; delete new_head; return res; }};
阅读全文
0 0
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 147. Insertion Sort List
- 微信公众号菜单编辑
- 如何扩大CMD窗口显示的行数
- Android–taskAffinity属性
- 第九周项目1---二叉树算法库
- SparkSQL的发展历史
- 147. Insertion Sort List
- XML Parser Error on line 16: 对实体 "useSSL" 的引用必须以 ';' 分隔符结尾。
- java消息服务使用总结(JMS:通过ActiveMQ实现)
- 我的Android进阶之旅------>Android采用AES+RSA的加密机制对http请求进行加密
- Ubuntu14.04LTS下升级gcc到gcc-6版本
- 深入理解Java中的final关键字
- VSCODE常用插件记录—不定期更新
- Oracle trunc()函数的用法
- leetcode: 89. Gray Code