leetcode之Insertion Sort List
2014-12-05 09:52
316 查看
问题描述如下:
Sort a linked list using insertion sort.
问题链接
cpp代码如下:
class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(head==NULL||head->next==NULL)return head;
ListNode node(0);
node.next=head;
ListNode* p=head->next,*r=head;
while(p){
ListNode* q=&node;
for(;q->next!=p&&q->next->val<p->val;q=q->next);
if(q->next==p){
r=p;
p=p->next;
}
else{
r->next=p->next;;
p->next=q->next;
q->next=p;
p=r->next;
}
}
return node.next;
}
};
Sort a linked list using insertion sort.
问题链接
cpp代码如下:
class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(head==NULL||head->next==NULL)return head;
ListNode node(0);
node.next=head;
ListNode* p=head->next,*r=head;
while(p){
ListNode* q=&node;
for(;q->next!=p&&q->next->val<p->val;q=q->next);
if(q->next==p){
r=p;
p=p->next;
}
else{
r->next=p->next;;
p->next=q->next;
q->next=p;
p=r->next;
}
}
return node.next;
}
};
相关文章推荐
- LeetCode--insertion-sort-list
- 【leetcode】147 Insertion Sort List
- LeetCode Insertion Sort List
- LeetCode Insertion Sort List
- leetcode_num179_Insertion Sort list
- 【LeetCode with Python】 Insertion Sort List
- Leetcode-insertion-sort-list ***
- [LeetCode]Insertion Sort List
- [LeetCode]Insertion Sort List
- leetcode-Insertion Sort List-147
- LeetCode ||Insertion Sort List
- leetcode 147 Insertion Sort List java 算法
- LeetCode--Insertion Sort List
- LeetCode(147) Insertion Sort List
- leetcode-147 Insertion Sort List
- leetcode---Insertion Sort List
- Insertion Sort List--LeetCode
- leetcode上的Sort a linked list using insertion sort
- Insertion Sort List - LeetCode 147
- [leetcode147]insertion sort list