博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
147. Insertion Sort List
阅读量:6903 次
发布时间:2019-06-27

本文共 2976 字,大约阅读时间需要 9 分钟。

题目:

Sort a linked list using insertion sort. 

Hide Tags
   
 

链接: 

题解:

链表插入排序,考察基本功。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;    }}

 

转载地址:http://ulpdl.baihongyu.com/

你可能感兴趣的文章
Android Logcat日志超链接(定位到本地代码)
查看>>
Android鬼点子-通过Google官方示例学NDK(1)
查看>>
跨平台开发框架和工具集锦
查看>>
HIVE扩展GIS函数
查看>>
iOS 全局设置系统导航栏返回按钮样式
查看>>
那些年收藏的技术文章(二) 云笔记篇
查看>>
【读】这一次,让我们再深入一点 - HTTP报文
查看>>
从零搭建nodejs服务器,配置域名解析+https证书 (以阿里云linux服务器为例)
查看>>
LinkedList遍历方式之坑
查看>>
Vue.js 组件之间的传值
查看>>
ES6 的几个小技巧
查看>>
Android前沿技术之热修复
查看>>
iOS 设置视图半透明而子控件不透明
查看>>
[短文速读-2] 重载/重写,动/静态分派?(重新修订)
查看>>
[iOS] [OC] 关于block回调、高阶函数“回调再调用”及项目实践
查看>>
Android自定义View之定点写文字
查看>>
Thrift RPC 系列教程(2)——全局变量&全局常量
查看>>
认识node核心模块--深入EventEmitter
查看>>
发布自己的开源库到Cocoapods
查看>>
sqlalchemy在python中的使用(关于查询)二
查看>>