您的位置:首页 > 其它

LeetCode之Insertion Sort List

2015-08-01 10:23 459 查看
/**
* 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 == nullptr || head->next == nullptr) return head;
ListNode node(-1);
node.next = head;
ListNode *p(head->next), *q;
head->next = nullptr;
while(p){
q = &node;
while(q->next && q->next->val < p->val) q = q->next;
ListNode *tmp = p;
p = p->next;
tmp->next = q->next;
q->next = tmp;
}
return node.next;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: