zoukankan      html  css  js  c++  java
  • 【数据结构】算法 LinkList (Insertion Sort List 链表插入排序)

    将一个单链表进行处理后,所得结果为一有序链表

    Solution:

    将原始链表逐个查询,插入新链表,在插入的同时对链表进行排序。时间复杂度O(n*n)

    public ListNode insertionSortList(ListNode head) {
             
            ListNode dummy = new ListNode(0);
             
            while (head != null) {
                ListNode node = dummy;
                while (node.next != null && node.next.val < head.val) {
                    node = node.next;
                }
                ListNode temp = head.next;
                head.next = node.next;
                node.next = head;
                head = temp;
            }
    
            return dummy.next;
        }
  • 相关阅读:
    5.6
    5.6
    4.30数据结构
    4.30
    POJ3616
    4.29
    4.28
    186周赛
    CF1267G Game Relics
    CF763E Timofey and our friends animals
  • 原文地址:https://www.cnblogs.com/dreamtaker/p/8519984.html
Copyright © 2011-2022 走看看