Skip to content
Go back

LeetCode 147 链表的插入排序

2 min read
Edit

题目描述:

Sort a linked list using insertion sort. Algorithm of Insertion Sort:

  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.

单链表的定义:

public class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

想法:

使用两条链表,一条是已经有序的链表 A,一条是待排序的链表 B。将节点从待排序的链表 B 依次插入已经有序的链表 A。

关键点在于想到使用两条链表。

代码:

class Solution {
    public ListNode insertionSortList(ListNode head) {
        if (head == null || head.next == null) { return head; }

        ListNode fakeHead= new ListNode(0); // because we need insert before head
        ListNode cur = head; // current node will be insert
        ListNode pre = null; // previous node of insert node
        ListNode next = null; // next node of current insert node

        while (cur != null) {
            // save next position
            next = cur.next;

            // find insert position
            pre = fakeHead;
            while (pre.next != null && pre.next.val < cur.val) {
                pre = pre.next;
            }

            // insert cur between pre and pre.next
            cur.next = pre.next;
            pre.next = cur;

            // move point forward
            cur = next;
        }

        return fakeHead.next;
    }
}

Edit
文章标题:LeetCode 147 链表的插入排序
文章链接: https://blog.guanglai.me/posts/leetcode-147-insertion-sort-list/

商业转载请联系站长获得授权,非商业转载请注明本文出处及文章链接。您可以自由地在任何媒体以任何形式复制和分发作品,也可以修改和创作,但分发衍生作品时必须采用相同的许可协议。

本文采用 CC BY-NC-SA 4.0 进行许可。


Previous Post
升级 Hexo 和对应的主题 NexT Theme 的版本
Next Post
使用 Docker 创建 MySQL 实验环境