Insertion Sort List
2015-09-24 21:14
453 查看
用插入排序对链表排序
您在真实的面试中是否遇到过这个题?
Yes
样例
Given
return
您在真实的面试中是否遇到过这个题?
Yes
样例
Given
1->3->2->0->null,
return
0->1->2->3->null
/** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */ class Solution { public: /** * @param head: The first node of linked list. * @return: The head of linked list. */ ListNode *insertionSortList(ListNode *head) { // write your code here if(head==NULL||head->next==NULL) return head; ListNode *dummy=new ListNode(-1); dummy->next=head; ListNode* pre=head; ListNode *cur=head->next; while(cur){ if(cur->val>=pre->val){ pre=pre->next; }else{ pre->next=cur->next; ListNode *restart=dummy; while(cur->val>=restart->next->val){ restart=restart->next; } cur->next=restart->next; restart->next=cur; } cur=pre->next; } return dummy->next; } };
相关文章推荐
- POJ1860 Currency Exchange 最短路bellman-ford
- 2016校招记
- 配制nginx
- Codevs p1014 装箱问题
- 敢创业但不敢上台演讲?
- Halcon学习之高斯混合模型
- lintcode-Segmemt Tree Build II-439
- java socket 长连接 nputStream的read 阻塞问题情况之一
- [一道搜狗输入法的面试题]C++转换构造函数和类型转换函数
- 搜狐2016笔试题
- Halcon学习之超盒分类
- 动态规划—输出所有的最长公共子序列
- HDU - 3008 Warcraft0(完全背包)
- Linux内核笔记——进程管理之执行体
- Halcon学习之K最近邻分类
- JavaScript程序编码规范
- Javascript 学习备忘(一)
- 使用git来控制版本 并分享到github上(自学或分享必备)
- Babelfish(二分)
- java 操作excel