leetcode之147. Insertion Sort List(C++解法)
2016-09-11 20:14
441 查看
题目:
Sort a linked list using insertion sort.
链表的插入排序
[b]********************************[/b]我是分割线[b]*****************************[/b]
/**
* 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* new_head = new ListNode(0);
ListNode* temp;
ListNode* cur;
new_head->next=head;
temp=new_head;
cur=head;
while(cur)
{
if(cur->next && cur->next->valval)
{
while(temp->next && temp->next->val < cur->next->val)
temp=temp->next;
ListNode* temptemp = temp -> next;
temp->next=cur->next;
cur->next=cur->next->next;
temp->next->next=temptemp;
temp=new_head;
}
else
cur=cur->next;
}
return new_head->next;
}
};
Sort a linked list using insertion sort.
链表的插入排序
[b]********************************[/b]我是分割线[b]*****************************[/b]
/**
* 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* new_head = new ListNode(0);
ListNode* temp;
ListNode* cur;
new_head->next=head;
temp=new_head;
cur=head;
while(cur)
{
if(cur->next && cur->next->valval)
{
while(temp->next && temp->next->val < cur->next->val)
temp=temp->next;
ListNode* temptemp = temp -> next;
temp->next=cur->next;
cur->next=cur->next->next;
temp->next->next=temptemp;
temp=new_head;
}
else
cur=cur->next;
}
return new_head->next;
}
};
相关文章推荐
- leetcode_效率题解_[python/C++]_147. Insertion Sort List(链表插入排序)
- LeetCode 147. Insertion Sort List插入排序链表的高效简单解法
- Leetcode_sort-list(c++ version)
- [C++]LeetCode: 125 Sort List (归并排序链表)
- LeetCode 147. Insertion Sort List(插入有序链表)
- Leetcode 147. Insertion Sort List (Medium) (cpp)
- 【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
- Sort List[leetcode] 由归并排序的递归和循环,到本题的两种解法
- LeetCode 之 Insertion Sort List — C++ 实现
- [leetcode] 【排序】 147. Insertion Sort List
- LeetCode 147 — Insertion Sort List(C++ Java Python)
- leetcode 147. Insertion Sort List
- [leetcode] 147. Insertion Sort List 解题报告
- leetcode 147. Insertion Sort List (Python版)
- leetcode_c++:链表:Insertion Sort List(147)