/**
* 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;
}
};
LeetCode之Insertion Sort List
最新推荐文章于 2019-08-18 11:38:40 发布