147. Insertion sort a linked list

147. Insertion sort a linked list

describe

Given the head of a single linked list, sort the linked list using insertion sort and return the head node of the sorted linked list .

example

insert image description here
Input: head = [-1,5,3,4,0]
Output: [-1,0,3,4,5]

answer

1. Find the insertion point from front to back

The basic idea of ​​insertion sorting is to maintain an ordered sequence . Initially, the ordered sequence has only one element. For each element in the unsorted sequence, find its corresponding insertion position in the ordered sequence and insert it. The ordered sequence The length of is increased by 1 until all elements are added to the ordered sequence.

in the case ofarrayInsertion sorting, the front part of the array is an ordered sequence, and each time the insertion position of the first element (the element to be inserted) behind the ordered sequence is found, the elements behind the insertion position in the ordered sequence are moved backward One bit , and then place the element to be inserted at the insertion position.

forlinked listIn other words, when inserting an element, you only need to update the pointer of the adjacent node , and you don’t need to move the element behind the insertion position backward like an array, so the time complexity of the insertion operation is O(1), but you need to traverse to find the insertion position The time complexity of the nodes in the linked list is O(n), so the total time complexity of the insertion sort of the linked list is still O ( n 2 ) O(n^2)O ( n2 )where n is the length of the linked list.

For a singly linked list, there is only a pointer to the next node, so it is necessary to traverse the nodes in the linked list from the head node of the linked list to find the insertion position .

The virtual head node dummyHead is used in the algorithm for auxiliary operations;

At the same time, the tail variable is used to record the tail node of the ordered linked list . For a new element, it can save time by comparing it with the tail node before inserting.

  • Time complexity: O ( n 2 ) O(n^2)O ( n2 )where n is the length of the linked list.
  • Space complexity: O ( 1 ) O(1)O(1)
class Solution {
    
    
public:
    ListNode* insertionSortList(ListNode* head) {
    
    
        ListNode* dummyHead = new ListNode(-1, head);
        ListNode* tail = head;                          // tail 为有序链表的尾节点
        ListNode* curr = head->next;
        while(curr != nullptr){
    
    
            if(curr->val < tail->val){
    
                      // 插入位置在有序序列中间
                ListNode* prev = dummyHead;
                while(curr->val > prev->next->val){
    
    
                    prev = prev->next;                  // 找到插入位置
                }
                tail->next = curr->next;                // 实现插入操作
                curr->next = prev->next;
                prev->next = curr;
            }else{
    
                                          // 当前节点大于尾节点,不需要插入,直接更新尾节点
                tail = curr;
            }
            curr = tail->next;                          // 遍历插入下一个节点
        }
        return dummyHead->next;
    }
};

Guess you like

Origin blog.csdn.net/qq_19887221/article/details/126368059