LeetCode 147. Insertion Sort List
LeetCode 147. Insertion Sort List
Solution1:我的答案
有点笨,有点慢
/*** 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) {if (!head) return NULL;if (!head->next) return head;ListNode* new_head = new ListNode(-1), *cur = head;while (cur) {ListNode *temp = cur->next;cur->next = NULL;my_insert(new_head, cur);cur = temp;}return new_head->next;}void my_insert (ListNode* &new_head, ListNode* &des) {if (!new_head->next || des->val <= new_head->next->val) {ListNode *temp = new_head->next;new_head->next = des;des->next = temp;return;} else {ListNode* cur = new_head->next;while (cur->next) {if (des->val >= cur->val && des->val <= cur->next->val) {des->next = cur->next;cur->next = des;return;}elsecur = cur->next;}cur->next = des;return;}}
};
Solution2:
参考网址:http://www.cnblogs.com/grandyang/p/4250107.html
/*** 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 *dummy = new ListNode(-1), *cur = dummy;while (head) {ListNode *t = head->next;//暂时保存头结点的下一个位置cur = dummy;while (cur->next && cur->next->val <= head->val) {cur = cur->next;}head->next = cur->next;cur->next = head;head = t;}return dummy->next;}
};
要反思为啥别人的代码写的如此简洁~~~
LeetCode 147. Insertion Sort List相关推荐
- LeetCode 147. Insertion Sort List 链表插入排序 C++/Java
LeetCode 147. Insertion Sort List 链表插入排序 C++/Java Sort a linked list using insertion sort. A graphic ...
- [Leetcode]147. Insertion Sort List
Sort a linked list using insertion sort. 链表的插入排序 思路,递归到链表尾,然后循环插入: 1 /** 2 * Definition for singly-l ...
- 147. Insertion Sort List
/** 147. Insertion Sort List * 2016-6-1 by Mingyang * insertion sort的基本思路要有,两个while循环,一次过,对于每一个值与pre ...
- 【leetcode】147. Insertion Sort List
Sort a linked list using insertion sort. 链表的插入排序. 需要创建一个虚拟节点.注意点就是不要节点之间断了. class Solution { public: ...
- LeetCode OJ 147. Insertion Sort List
Sort a linked list using insertion sort. 对链表使用插入排序还是很简单的,从链表中拆下一个节点,然后把它插入到已经排序的部分的链表中,直到所有节点都被插入.代码 ...
- leetcode之Insertion Sort List
Sort a linked list using insertion sort. 这道题是用链表做插入排序.虽然之前数据结构中学习了链表的插入删除等,但那些都是最简单的基本操作,只是给出一个节点,直接 ...
- [Leetcode] Insertion Sort List
Sort a linked list using insertion sort. 虽然算法很简单,但是链表操作起来实正是烦啊,特别要注意各种边界条件. 1 /** 2 * Definition for ...
- leetcode day2 -- Sort List Insertion Sort List
1.Sort List Sort a linked list in O(n log n) time using constant space complexity. 分析:对链表排序不是第一次见,但是 ...
- Leetcode Insertion Sort List
Sort a linked list using insertion sort. 链表的插入排序,其实有2种特殊情况: 1.插入的值插入到已排序的末尾. 2.插入的值插入到已排序的最前端. 主要设置了 ...
最新文章
- linux下不同程序如何协同,Linux程序员怎么和Exchange协同工作
- FileChannel应用实例——本地文件读数据(假定文件已存在)
- C# 通用Clone
- HIbernate的优缺点
- 使用 xCAT 简化 AIX 集群的部署和管理
- Java 8 CompletableFuture 浅入
- Junit4集成到Maven工程
- 【kafka】flink 发送 数据到 kafka 报错 Memory records is not writable
- lstm原始论文_命名实体识别NER论文调研
- 山东大学-WIZnet网络控制实验室揭牌仪式成功举行
- python输入人名对话_对话框中的用户输入
- AndroidTV开发9远程adb调试设备
- Http 资源服务器 搭建 HFS
- dbm与mysql_DBM数据库 | 学步园
- 资源分享(nlp、kaggle、pytorch、datawhale)
- 常用英语几大构词方法
- 寒假第三周网页制作总结
- 解决0xc000012f 损坏的映像rasman.dll没有被指定在windows运行问题
- C - Super Mario(分块+二分)
- 读我们的学科——计算机专业学习浅谈
热门文章
- 设计模式练习:Decorate模式
- 取得人生成功的40条定律
- Laplacian of Gaussian (LOG) 高斯拉普拉斯算子
- html div右下浮动,div 初始化及左右浮动
- html 星空效果,html5 canvas炫酷旋转银河系星空背景特效
- mysql安全模式什么意思_mysql安全模式指的是什么意思
- 拼接播放地址_西安户外did拼接屏批发业务广泛_金伟达电子
- 爆炸性环境设备通用要求标准_防爆电气设备的适用环境及温度要求
- word计算机排版怎么选,Word选择题选项对齐排版方法 查找替换工具搞定
- macbook交叉编译linux,mac交叉编译到Linux报错