Insertion Sort List
来源:互联网 发布:社员网 知乎 编辑:程序博客网 时间:2024/04/27 04:34
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) { ListNode *p1=NULL,*p2=NULL,*pre1=NULL,*pre2 =NULL;if(head) { pre2=head;//p2的前驱p2=head->next;}while(p2){//从第二个元素开始pre1=NULL;//p1的前驱p1=head;while(p1!=p2){if(p1->val > p2->val){ if(pre1){//不是头元素pre2->next=p2->next;p2->next = p1;pre1->next = p2;p2 = pre2;}else{//是头元素pre2->next = p2->next;p2->next = p1;head = p2;p2 = pre2;}break;}else{ pre1=p1;p1=p1->next;}}pre2=p2;p2=p2->next;} return head;}};
0 0
- Insertion Sort List | leetcode
- Leetcode: Insertion Sort List
- [LeetCode] Insertion Sort List
- Insertion Sort List
- Leetcode: Insertion Sort List
- [LeeCode] Insertion Sort List
- leetcode-Insertion Sort List
- [LeetCode]Insertion Sort List
- Insertion Sort List
- 【LeetCode】Insertion Sort List
- LeetCode | Insertion Sort List
- LeetCode - Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- [LeetCode] Insertion Sort List
- list - insertion sort - AC
- storm文档(4)----开发环境环境搭建
- Git冲突:commit your changes or stash them before you can merge. 解决办法
- NMON文件重要指标详解
- ViBe运动检测算法(visual background extractor)
- JDE scheduler job-----How to start Scheduler job
- Insertion Sort List
- Oracle 用Execute执行带有参数的字符串语句。
- 通配符的匹配很全面, 但无法找到元素 'jdbc:embedded-database' 的声明
- unity3d学习之数组习题
- 在网上找到的一个附件上传功能,自己完善了一下,记录防止忘记
- iOS sqlite3 具体代码粘贴
- ios 简便的提醒管理类 可以当闹钟用
- 市场上到底有多少款手机?(1)
- 23种设计模式(超级简洁)