zoukankan      html  css  js  c++  java
  • 147. Insertion Sort List

    题目:

    Sort a linked list using insertion sort. 

    Hide Tags
     Linked List Sort 

    链接: https://leetcode.com/problems/insertion-sort-list/

    题解:

    链表插入排序,考察基本功。worst case时间复杂度是O(n2) , best case时间复杂度是O(n)。   好久没做题最近完全懈怠了,要继续努力啊。

    Time Complexity - O(n2) (worst case), Space Complexity - O(n)。

    public class Solution {
        public ListNode insertionSortList(ListNode head) {
            if(head == null || head.next == null)
                return head;
            ListNode dummy = new ListNode(-1);
            
            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;
        }
    }

    Update:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode insertionSortList(ListNode head) {
            if(head == null || head.next == null)
                return head;
            ListNode dummy = new ListNode(-1);
            
            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;
        }
    }

    二刷:

    跟一刷使用的节点交换顺序不太一样。不过大体方法差不多。在head不为空的情况下,先设一个dummy的reference copy - node,从头比较node.next.val 和head.val来找到insert的位置,再倒腾几下就好了。

    Java:

    Time Complexity - O(n2) (worst case), Space Complexity - O(n)。

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode insertionSortList(ListNode head) {
            ListNode dummy = new ListNode(-1);
            while (head != null) {
                ListNode node = dummy;
                while (node.next != null && node.next.val < head.val) {
                    node = node.next;
                }
                ListNode tmp = node.next;
                node.next = head; 
                head = head.next;
                node.next.next = tmp;
            }
            return dummy.next;
        }
    }

    三刷:

    Java:

    Time Complexity - O(n2) (worst case), Space Complexity - O(1)。

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode insertionSortList(ListNode head) {
            ListNode dummy = new ListNode(-1);
            ListNode tmp = null, node = dummy;;
            while (head != null) {
                node = dummy;
                while (node.next != null && node.next.val < head.val) node = node.next;
                tmp = node.next;
                node.next = head;
                head = head.next;
                node = node.next;
                node.next = tmp;
            }
            return dummy.next;
        }
    }
  • 相关阅读:
    在SpringBoot或者Spring项目中实现最原始的分页功能
    element ui 弹出组件的遮罩层在弹出层的上面的解决方法
    vue中ref的使用(this.$refs获取为undefined)
    echarts的图表根据父容器大小的改变而改变(弹窗easy-ui的window窗口)
    vue项目使用history模式打包应该注意的地方
    echarts数据变了不重新渲染,以及重新渲染了前后数据会重叠渲染的问题
    element-ui的layout将24等分换为48等分
    vue中解决拖动和点击事件的冲突
    制作首页的显示列表。
    发布功能完成。
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4489578.html
Copyright © 2011-2022 走看看