147-insertion-sort-list

2019-07-20  本文已影响0人  Twopothead

147

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* insertionSortList(struct ListNode* head){
    for (struct ListNode *p = head; p != NULL; p=p->next)
    {
        int key = p->val;
        struct ListNode *q = head;
        while (q && q!=p)
        {
            if(q->val>key){
                int a = q->val;
                q->val = key;
                key = a;
            }
            q=q->next;
        }
        p->val = key;
    }
    return head;
}
上一篇 下一篇

猜你喜欢

热点阅读