insertion-sort-list——链表、插入排序、链表插入
2017-06-07 16:32
633 查看
Sort a linked list using insertion sort.
PS:需要考虑left为head且只有一个数时,此时left->==NULL,若right<left则应更新left。
比较p->next->val与right->val以此来避免需要记录preNode
PS:需要考虑left为head且只有一个数时,此时left->==NULL,若right<left则应更新left。
比较p->next->val与right->val以此来避免需要记录preNode
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *insertionSortList(ListNode *head) { 12 ListNode *left=NULL,*right=head; 13 if(head==NULL) 14 return head; 15 left=head; 16 right=head->next; 17 left->next=NULL; 18 19 while(right!=NULL){ 20 ListNode *p=left; 21 ListNode *cur=right; 22 23 while(p!=NULL&&p->next!=NULL&&p->next->val<right->val) 24 p=p->next; 25 right=right->next; 26 if(p->val<=cur->val){ 27 cur->next=p->next; 28 p->next=cur; 29 }else{ 30 cur->next=p; 31 left=cur; 32 } 33 34 35 } 36 return left; 37 } 38 };
相关文章推荐
- 【lintcode】——链表插入排序-Insertion Sort List
- [C++]LeetCode: 126 Insertion Sort List (插入排序链表)
- LeetCode-Insertion Sort List-链表插入排序-链表操作
- Insertion Sort List ——链表插入排序
- Insertion Sort List——链表的插入排序
- 链表的插入排序 Insertion Sort List
- Insertion Sort List 链表的插入排序
- leetcode Insertion Sort List(链表插入排序)
- Insertion Sort List 使用插入排序将链表排序
- Insertion Sort List(单链表插入排序)
- LeetCode:Insertion Sort List//链表插入排序
- Leetcode Insertion Sort List 插入排序链表
- leetcode——Insertion Sort List 对链表进行插入排序(AC)
- LeetCode Insertion Sort List 链表插入排序
- [Leetcode] insertion sort list 链表插入排序
- LeetCode 147 Insertion Sort List(链表插入排序)
- Insertion Sort List 链表插入排序
- 【LeetCode-面试算法经典-Java实现】【147-Insertion Sort List(链表插入排序)】
- [LeetCode] Insertion Sort List 链表插入排序
- [LeetCode] Insertion Sort List 单向链表插入排序