insertion-sort-list
2017-03-22 11:18
351 查看
题目描述
Sort a linked list using insertion sort.
链表的插入排序,不难
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(head==NULL|| head->next==NULL) return head; ListNode* prehead=new ListNode(INT_MIN); prehead->next=head; ListNode* phead=prehead; ListNode* cur=head; ListNode* next=head->next; while(next!=NULL) { if(next->val>=cur->val) { cur=cur->next; next=next->next; } else { ListNode* temp=next; next=temp->next; cur->next=next; while(phead->next!=cur) { if(temp->val>phead->next->val) phead=phead->next; else break; } temp->next=phead->next; phead->next=temp; phead=prehead; } } return prehead->next; } };
相关文章推荐
- Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- LeetCode -- Insertion Sort List
- 【LeetCode with Python】 Insertion Sort List
- LeetCode - Insertion Sort List
- Merge Sort Array --> Merge Sort Linked List --> Insertion Sort Array --> Insertion sort Linked List
- Insertion Sort List 使用插入排序将链表排序
- LeetCode(147) Insertion Sort List
- Sort a linked list using insertion sort
- Leetcode Insertion Sort List 解题报告
- Insertion Sort List
- Insertion Sort List
- LeetCode:Insertion Sort List//链表插入排序
- 【Leetcode】Insertion Sort List
- [LeetCode]Insertion Sort List
- 147 Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- Insertion Sort List