LeetCode 147. Insertion Sort List(对链表进行插入排序)--c语言

147. Insertion Sort List

Sort a linked list using insertion sort.

LeetCode 147. Insertion Sort List(对链表进行插入排序)--c语言_第1张图片
A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list

Algorithm of Insertion Sort:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
It repeats until no input elements remain.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4
Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

 

解题思路:

法一:

/*
执行用时 : 52 ms, 在Insertion Sort List的C提交中击败了81.90% 的用户
内存消耗 : 7.8 MB, 在Insertion Sort List的C提交中击败了95.65% 的用户
*/
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* insertionSortList(struct ListNode* head){
    
    //特殊情况
    if(head == NULL || head->next == NULL){
        return head;
    }
    //新增“哑结点”,用于链接有序区
    struct ListNode* L = (struct ListNode*)malloc(sizeof(struct ListNode));
    L->next = head;
    
    struct ListNode* p = head->next;//指向第二个结点,用于遍历待处理区
    struct ListNode* q = L;//用于遍历有序区
    head->next = NULL;//将有序区与待处理区断开
    
    while(p != NULL){
        q = L;//每次q都重新指向“哑结点”
        struct ListNode* curNode = p;//curNode为当前处理结点
        p = p->next;//p指向下一个待处理节点
        
        //对所有结点查找待插入位置并插入
        while(q->next != NULL){
            if(q->next->val > curNode->val){
                curNode->next = q->next;
                q->next = curNode;
                break;
            }
            q = q->next;
        }
        //待插入位置是有序区的末尾
        if(q->next == NULL){
            q->next = curNode;
            curNode->next =NULL;
        }
    }
    
    return L->next;

}

法二:

/*
执行用时 : 12 ms, 在Insertion Sort List的C提交中击败了100.00% 的用户
内存消耗 : 8.1 MB, 在Insertion Sort List的C提交中击败了19.57% 的用户
*/

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


struct ListNode* insertionSortList(struct ListNode* head){
    
    //特殊情况
    if(head == NULL || head->next == NULL){
        return head;
    }
    //新增“哑结点”,用于链接有序区
    struct ListNode* L = (struct ListNode*)malloc(sizeof(struct ListNode));
    L->next = head;
    
    struct ListNode* p_tail = head;//指向有序区尾结点
    struct ListNode* q = L;//用于遍历有序区
    
    while(p_tail->next != NULL){
        struct ListNode* curNode = p_tail->next;
        
        if(curNode->val >= p_tail->val){
            p_tail = p_tail->next;
        }
        else{
            while(q != p_tail){
                if(q->next->val > curNode->val){//找到了插入位置,插入
                    p_tail->next = curNode->next;
                    curNode->next = q->next;
                    q->next = curNode;
                    q = L;
                    break;
                }
                q = q->next;//没找到插入位置,继续找,直到q指向尾结点
            }//while
            
        }
    }
    
    return L->next;

}

 

你可能感兴趣的:(LeetCode,(力扣),算法,(Algorithm),C语言,数据结构,(Data,Structure),147.,Insertion,Sort,List,对链表进行插入排序,LeetCode,c语言)