Insertion sort on the linked list. Starting from the first element, the linked list can be considered partially sorted. At each iteration, remove an element from the input data and insert it in-place into the sorted linked list.

The original title:

Paste the code first:

public class Solution147 {
    public ListNode insertionSortList(ListNode head) {
        //当链表为空时,不需排序,直接返回
        if (head == null){
            return head;
        }
        //创建一个虚拟头节点,方便处理头节点
        ListNode newhead = new ListNode(-1);
        newhead.next = head;
        //ret为待排序的节点
        ListNode ret = head.next;
        //进入循环,直到待排序的节点为空(不存在待排序的节点)
        while (ret != null){
            //情况1:头节点小于等于待排序节点
            if (head.val <= ret.val){
                head = head.next;
            //情况2:头节点大于待排序节点
            }else {
                //新建一个cur节点来寻找前面有序链表中大于待排序节点的节点,插入它的前面
                ListNode cur = newhead;
                while (cur.next.val <= ret.val){
                    cur = cur.next;
                }
                head.next = ret.next;
                ret.next = cur.next;
                cur.next = ret;
            }
            ret = head.next;
        }
        return newhead.next;
    }
}

Analysis of ideas:

In this question, we are required to use insertion sort to solve the problem, so what is insertion sort?

1. Insertion sort is iterative, moving only one element at a time until all elements can form an ordered output list.
2. In each iteration, insertion sort removes only one element to be sorted from the input data, finds its proper position in the sequence, and inserts it.
3. Repeat until all input data is inserted.

As can be seen from the above, we first need to mark a node in the linked list, which is recorded as the ret node to be sorted. Compare this node with the head node of the linked list to determine the size. If the ret node is greater than or equal to the head node, it means that the node should be ranked behind the head node, so we get a small ordered linked list. The ret node is the tail node of the ordered linked list.

Move both the head node and the ret node one bit backward, so that the head node becomes the tail node in the ordered list again (there was no node in front of the head node, only it was one node, and the default was the tail node of the ordered list).

Constantly compare the following node with the tail node of the previous ordered list, and if it is larger than the tail node, it will be at the back. The node of the linked list to be sorted is inserted in front of it.

As shown in the figure below (if the picture is not good, don’t blame it), if ret is greater than or equal to head, there is no need to insert; if ret is less than head, enter the ordered list in front of head and sort it again to find a suitable position (find a node larger than ret, put it here the front of the node):

As for why not find the node smaller than the ret node in the ordered linked list in front of it and rank behind it? Because if there is a node larger than ret and smaller than head in the back, it will be confused when entering the previous linked list and cannot be sorted again.

Guess you like

Origin blog.csdn.net/weixin_56960711/article/details/122453403