[leetcode] Insertion Sort List
2015-06-09 13:35
344 查看
From : https://leetcode.com/problems/insertion-sort-list/
Sort a linked list using insertion sort.
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) { if(!head || !head->next) return head; ListNode *index, *cur=head->next, *p, *pre; ListNode *phead = new ListNode(0); phead->next = head; head->next = NULL; while(cur) { int val = cur->val; pre = phead; index = pre->next; p = cur->next; while(index && index->val<=val) { pre=index; index=index->next; } cur->next = index; pre->next = cur; cur = p; } head = phead->next; delete phead; return head; } };
相关文章推荐
- Axure RP Extension for Chrome离线版下载
- 矩形周长并 hdu 1828 Picture
- 方法说---mac下安装homebrew与mongodb
- Spring + JdbcTemplate + JdbcDaoSupport examples
- POJ 1095
- POJ 1089
- POJ 1088
- C#进阶ADO.NET基础三 案例:登录框、导入文档、省市联动
- Android自定义View基础(1)
- 在希望的田野上--生物柴油(Biodiesel)光明的未来
- java web汉字传参的解决办法
- leetcode:Multiply Strings
- POJ 1094
- 页面文案展开和收缩样式的操作
- Android4.0反编译笔记
- 路由路由脚本自动切换网关
- POJ 1087
- 黑马程序员---IOS基础---内存管理及内存分区
- 闲置笔记本锂电池修复方法
- C#中this用法汇总