leetcode[147]:Insertion Sort List

来源:互联网 发布:php nginx 虚拟机配置 编辑:程序博客网 时间:2024/04/28 18:03

Insertion Sort List

Sort a linked list using insertion sort.

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* insertionSortList(struct ListNode* head) {    struct ListNode *L,*p,*tmp;    if(!head || !head->next) return head;    p = head->next;    L=head;    L->next = NULL;    while(p)    {        tmp=p;        p=p->next;        if(tmp->val < head->val)        {            tmp->next=head;            head=tmp;        }        else{            while(tmp->val > L->val)            {                if(!L->next) break;                if(L->next->val < tmp->val) L=L->next;                else break;            }            tmp->next=L->next;            L->next=tmp;        }        L=head;    }    return head;}

直接插入排序。双指针?

0 0
原创粉丝点击