[leetcode 147]Insertion Sort List
来源:互联网 发布:淘宝开店银行卡验证 编辑:程序博客网 时间:2024/04/28 13:53
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(NULL == head || NULL == head->next){return head;} ListNode *runner = head; ListNode *prerunner = head; ListNode *handle = head->next; ListNode *prehandle = head; while(handle) { runner = head; prerunner = head; while(runner!= handle && handle->val>=runner->val) { prerunner = runner; runner = runner->next; } if(runner == handle) { prehandle = handle;handle = handle->next; } else { prehandle->next = handle->next; if(runner == head) { head = handle; handle->next = runner; } else { handle->next = runner; prerunner->next = handle; } handle = prehandle->next; } } return head; }};
class Solution {public: ListNode *insertionSortList(ListNode *head) { if(NULL == head || NULL == head->next){return head;} ListNode firstnode(0); firstnode.next = head; ListNode *handle = head->next; ListNode *prehandle = head; while(handle) { ListNode *prerunner = &firstnode; ListNode *runner = firstnode.next; //head may change while(runner!= handle && handle->val>=runner->val) { prerunner = runner; runner = runner->next; } if(runner == handle) { prehandle = handle;handle = handle->next; } else { prehandle->next = handle->next; handle->next = runner; prerunner->next = handle; handle = prehandle->next; } } return firstnode.next; }};
0 0
- LeetCode(147) Insertion Sort List
- LeetCode: Insertion Sort List [147]
- LeetCode 147 Insertion Sort List
- [leetcode 147] Insertion Sort List
- LeetCode 147 Insertion Sort List
- LeetCode 147 Insertion Sort List
- [leetcode 147]Insertion Sort List
- leetcode-147 Insertion Sort List
- [LeetCode 147]Insertion Sort List
- Leetcode[147]-Insertion Sort List
- Insertion Sort List - LeetCode 147
- leetcode[147]:Insertion Sort List
- LeetCode(147) Insertion Sort List
- leetcode 147: Insertion Sort List
- [Leetcode]#147 Insertion Sort List
- leetCode #147 Insertion Sort List
- LeetCode 147 Insertion Sort List
- LeetCode 147:Insertion Sort List
- JVM必备指南
- mysql源码阅读笔记-mysql 同步机制
- openfire3.9.3 源码部署及运行
- Maven使用memcached
- eclipse开发jsp,网页出现乱码
- [leetcode 147]Insertion Sort List
- 转自高手关于SQL 锁的叙述。。
- 编写一个程序,从标准输入读取几行输入。每行输入都要打印到标准输出上,前面要加上行号。
- windows环境下wampserver的配置教程——超级详细
- 动态链接库DLL
- leetcode Reverse Nodes in k-Group
- Unicode字符用PHP进行转码
- 红蜻蜓抓图精灵 网页截图 ,Ctrl+Shift+C
- Kmeans、Kmeans++和KNN算法比较