• [LeetCode] 147. Insertion Sort List


    Given the head of a singly linked list, sort the list using insertion sort, and return the sorted list's head.

    The steps of the insertion sort algorithm:

    1. Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.
    2. 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.
    3. It repeats until no input elements remain.

    The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.

    对链表进行插入排序。

    插入排序算法:

    插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。
    每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。
    重复直到所有输入数据插入完为止。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/insertion-sort-list
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    题目就是题意,将一个乱序的 linked list 通过插入排序的方式重新排序。

    这道题不涉及算法,就是按照插入排序的思路将链表排序。当遍历链表的时候,你需要看每两个 node 之间是不是满足后一个 node.val >= 前一个 node.val,如果不满足,就需要将后一个 node 提出来,再从链表的头节点开始一个个遍历,最终放到合适的位置上去。其余解释请参见代码注释。

    时间O(n^2)

    空间O(1)

    Java实现

     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) { val = x; }
     7  * }
     8  */
     9 class Solution {
    10     public ListNode insertionSortList(ListNode head) {
    11         // corner case
    12         if (head == null || head.next == null) {
    13             return head;
    14         }
    15         // normal case
    16         ListNode dummy = new ListNode(0);
    17         dummy.next = head;
    18         // cur节点用来遍历整个list
    19         ListNode cur = head;
    20         ListNode temp = null;
    21         ListNode prev = null;
    22         // 4->2->1->3
    23         while (cur != null && cur.next != null) {
    24             // 如果节点顺序对,就接着往后走
    25             if (cur.val <= cur.next.val) {
    26                 cur = cur.next;
    27             } else {
    28                 // temp保存那个较小的节点值
    29                 // 比如例子中一开始的2
    30                 temp = cur.next;
    31                 // 4 -> 1
    32                 cur.next = temp.next;
    33                 // prev又从头开始遍历链表
    34                 prev = dummy;
    35                 while (prev.next.val <= temp.val) {
    36                     prev = prev.next;
    37                 }
    38                 // 2 -> 4
    39                 temp.next = prev.next;
    40                 // dummy -> 2
    41                 prev.next = temp;
    42             }
    43         }
    44         return dummy.next;
    45     }
    46 }

    LeetCode 题目总结

  • 相关阅读:
    ArcMap导出图层属性为excel表
    ArcMap面转为线
    vue(18)声明周期函数
    geoserver发布mbtiles文件
    docker部署geoserver
    vue(17)组件插槽slot的使用
    vue(16)父子组件之间直接相互访问
    MySQL常用查询语句积累
    数据库的基本设计
    HashMap学习
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13737509.html
Copyright © 2020-2023  润新知