LeetCode | Insertion Sort List
2016-08-12 16:55
459 查看
Sort a linked list using insertion sort.
考察链表的插入排序
一开始在root.next=head;上卡了很久,后来想一想其实这是在一条新的list上进行的操作。
每次找一个节点,并将其插入的新的list上,是所谓插入排序
考察链表的插入排序
一开始在root.next=head;上卡了很久,后来想一想其实这是在一条新的list上进行的操作。
每次找一个节点,并将其插入的新的list上,是所谓插入排序
class Solution { public: ListNode* insertionSortList(ListNode* head) { ListNode root(INT_MIN); // root.next=head; for(ListNode* cur=head;cur;){ //找到插入数据的点 ListNode* pos=&root; while(pos->next && pos->next->val < cur->val) pos=pos->next; //找到第一个pos->next值比cur->val大的元素 ListNode* next=cur->next; cur->next=pos->next; pos->next=cur; cur=next; } return root.next; } };
相关文章推荐
- [LeetCode] Insertion Sort List 单向链表插入排序
- 【LeetCode】Insertion Sort List
- Leetcode: Insertion Sort List
- LeetCode(2):insertion-sort-list
- [leetcode]Insertion Sort List
- leetcode题目解答---Insertion Sort List
- [Leetcode] insertion sort list 链表插入排序
- leetcode之Insertion Sort List
- leetcode--Insertion Sort List
- leetcode:Insertion Sort List
- LeetCode Insertion Sort List
- leetcode - Insertion Sort List
- LeetCode---Insertion Sort List
- LeetCode - Insertion Sort List
- 【LeetCode】-Insertion Sort List
- LeetCode Insertion Sort List
- leetcode做题总结,题目Insertion Sort List 2013/11/12
- Leetcode习题:Insertion Sort List
- LeetCode147—Insertion Sort List
- [leetcode 147]Insertion Sort List