您的位置:首页 > 其它

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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: