Insertion Sort List

来源:互联网 发布:见微数据 搜披露 编辑:程序博客网 时间:2024/06/08 04:42

Sort a linked list using insertion sort.

class Solution {public:    ListNode* insertionSortList(ListNode* head) {        if(!head) return NULL;        ListNode* pre=new ListNode(-1),*p=head,*q;        while(head){            p=head->next;            head->next=NULL;            q=pre;            while(q->next&&q->next->val<head->val  )                    q=q->next;             head->next=q->next;             q->next=head;             head=p;            }        return pre->next;    }};
0 0
原创粉丝点击