leetcode_147_Insertion Sort Lis
来源:互联网 发布:iphone数据恢复软件 编辑:程序博客网 时间:2024/04/30 11:41
欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢
Insertion Sort List
Sort a linked list using insertion sort.
class Solution {public: ListNode *insertionSortList(ListNode *head) {if( head==NULL || head->next==NULL )return head;ListNode *p = new ListNode(-1);p->next = head;ListNode *pre = head;ListNode *cur = head->next;while(cur){if(cur->val>=pre->val){pre = cur;cur = cur->next;}else{//first find the insert position ListNode *inserpre = p;ListNode *insercur = p->next;while( insercur->val < cur->val ){inserpre = insercur;insercur = insercur->next;}pre->next = cur->next;cur->next = insercur;inserpre->next = cur;cur = pre->next;}}head = p->next;return head; }};
#include<iostream>#include<stack>using namespace std;#define N 5struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};class Solution {public: ListNode *insertionSortList(ListNode *head) {if( head==NULL || head->next==NULL )return head;ListNode *p = new ListNode(-1);p->next = head;ListNode *pre = head;ListNode *cur = head->next;while(cur){if(cur->val>=pre->val){pre = cur;cur = cur->next;}else{ListNode *inserpre = p;ListNode *insercur = p->next;while( insercur->val < cur->val ){inserpre = insercur;insercur = insercur->next;}pre->next = cur->next;cur->next = insercur;inserpre->next = cur;cur = pre->next;}}head = p->next;return head; }};ListNode *creatlist(){ListNode *head = NULL;ListNode *p;for(int i=0; i<N; i++){int a;cin>>a;p = (ListNode*) malloc(sizeof(ListNode));p->val = a;p->next = head;head = p;}return head;}int main(){ListNode *list = creatlist();Solution lin;ListNode *outlist = lin.insertionSortList ( list );for(int i=0; i<N; i++){cout<<outlist->val;outlist = outlist->next;}}
1 0
- leetcode_147_Insertion Sort Lis
- HDU 1051 Wooden Sticks(LIS+sort)
- Codeforces 340D Bubble Sort Graph【LIS变形】
- LIS
- LIS
- LIS
- LIS
- LIS
- LIS
- LIS
- LIS
- LIS
- LIS
- LIS
- LIS
- LIS
- LIS++
- lis
- 【Ubuntu】Ubuntu 硬盘“分区”图文教程(用于光盘,U盘安装Ubuntu)
- Json学习笔记
- Animation 动画
- spring bean源码简单解析
- java编程思想并发学习笔记(rocket and rockets)
- leetcode_147_Insertion Sort Lis
- 模仿淘宝京东数量选择器
- 设计模式学习笔记七:策略模式
- Vaadin系列(二) 应用开发:Table 组件
- Openstack Keystone 认证流程(五)--路由
- Team Queue UVA 540 queue+map LRJ做法
- 详谈排序算法之插入类排序(两种思路实现希尔排序)
- Java语言基础——第三回 Eclipse使用技巧和包的用途
- linux bio学习总结