[leetcode] 【排序】 147. Insertion Sort List
来源:互联网 发布:淘宝15天退换货图片 编辑:程序博客网 时间:2024/05/22 14:41
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 newstart(INT_MIN); for(auto cur=head;cur!=NULL;) { ListNode *p=&newstart; while(p->next!=NULL&&cur->val > p->next->val) p=p->next; ListNode *temp=p->next; p->next=cur; cur=cur->next; p->next->next=temp; } return newstart.next; }};
0 0
- [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
- 147. Insertion Sort List LeetCode
- 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
- [LeetCode] 147. Insertion Sort List
- Leetcode-147. Insertion Sort List
- [LeetCode]147. Insertion Sort List
- LeetCode 147. Insertion Sort List
- Spring4+WebSocket搭建
- Stencil Buffer(模板缓冲区)
- Java工具类——实现gif图片缩放与剪切功能
- 第一篇博客
- 数组类型不同解析问题探究
- [leetcode] 【排序】 147. Insertion Sort List
- 比较简单的win32 OpenGL 程序
- XMPP协议实现原理介绍
- 利用ZXing生成二维码的工具类
- python中的is ,is not
- leetcode 20. Valid Parentheses
- 基于 docker 的 UI 自动化测试实践
- jquery.validate在IE8下,点击提交不进行验证的解决方案
- ios多线程使用精讲