[leetcode] Insertion Sort List
2015-07-20 15:47
399 查看
#include<iostream> using namespace std; struct ListNode { int value; ListNode* next; ListNode(int value):value(value),next(NULL){}; }; ListNode* Insertsort(ListNode* head) { if(!head) return NULL; ListNode* dump=new ListNode(INT_MIN); dump->next=head; bool flag=false; for(ListNode* pre=dump;pre->next!=NULL;) { ListNode* p=NULL; for(ListNode* cur=dump;cur!=pre->next;p=cur,cur=cur->next) { if(pre->next->value>cur->value) { continue; } p->next=pre->next; pre->next=pre->next->next; p->next->next=cur; flag=true; break; } if(!flag) pre=pre->next; else flag=false; } return dump->next; } void main() { int a[10]={2,5,1,5,3,2,4,9,8,1}; ListNode* head=new ListNode(a[0]); ListNode* cur=head; for(int i=1;i<10;i++) { ListNode* node=new ListNode(a[i]); cur->next=node; cur=cur->next; } head=Insertsort(head); while(head) { cout<<head->value<<' '; head=head->next; } }
相关文章推荐
- 【代码片段】隐藏与显示部分列表
- 史上最全的CSS hack方式一览
- mac之ssh免登陆验证、ssh-keygen
- UIScrollView手动轮播图
- 打造自己的LINQ Provider(上):Expression Tree揭秘
- jquery.cookie中的操作
- Java 之Random用法及实例
- VPS服务器如何配置伪静态化(.htaccess配置)
- python访问MySQL数据库
- SC Create 创建一个Windows系统服务
- HDOJ 1000 (C语言第一个程序~镇博之作)
- 利用函数来获得本机所有的端口
- RGB888 TO BT1120 转换
- java io流
- SQLite数据库的使用
- 百度在线语音识别接入经验
- ul li a active jquery.cookie.js
- NOIP2012-普及组复赛-第一题-质因数分解
- 茅台高管年薪200多万
- zabbix监控系统的实际应用(客户端安装-基本应用设置-报警提醒篇)