LeetCode:Insertion Sort List
2014-08-26 17:05
344 查看
Sort a linked list using insertion sort
ListNode *insertionSortList(ListNode *head)
{
if(!head || !head->next)
return head;
ListNode* result =head;
ListNode* p = head;
ListNode* pre =NULL;
head = head->next;
result->next = NULL;
while(head != NULL)
{
p = result;
while(p != NULL && head->val > p->val)
{
pre=p;
p=p->next;
}
if(p == result)
{
result = head;
head = head->next;
result -> next = p;
}
else
{
pre->next = head;
head = head->next;
pre->next->next = p;
}
}
return result;
}
已AC 276ms
ListNode *insertionSortList(ListNode *head)
{
if(!head || !head->next)
return head;
ListNode* result =head;
ListNode* p = head;
ListNode* pre =NULL;
head = head->next;
result->next = NULL;
while(head != NULL)
{
p = result;
while(p != NULL && head->val > p->val)
{
pre=p;
p=p->next;
}
if(p == result)
{
result = head;
head = head->next;
result -> next = p;
}
else
{
pre->next = head;
head = head->next;
pre->next->next = p;
}
}
return result;
}
已AC 276ms
相关文章推荐
- 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
- The Solution to LeetCode 147 Insertion Sort List
- Leetcode[147]-Insertion Sort List
- leetcode-Insertion Sort List
- LeetCode - Insertion Sort List
- Insertion Sort List ---leetcode---Runtime Error
- LeetCode 147:Insertion Sort List
- Leetcode习题:Insertion Sort List