147. Insertion Sort List (List)
Sort a linked list using insertion sort.
class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(!head || !head->next) return head;
ListNode *tail =head->next;
head->next = NULL;
ListNode *current;
ListNode *tmp;
while(tail){
if(tail->val < head->val){
tmp = tail->next;
tail->next = head;
head = tail;
tail = tmp;
continue;
}
current = head;
while(current->next && tail->val >= current->next-> val) current = current->next;
tmp = tail->next;
tail->next = current->next;
current->next = tail;
tail = tmp;
}
return head;
}
};
147. Insertion Sort List (List)的更多相关文章
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- [LeetCode] 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- LeetCode OJ 147. Insertion Sort List
Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...
- Java for LeetCode 147 Insertion Sort List
Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...
- 147. Insertion Sort List
Sort a linked list using insertion sort. 代码如下: /** * Definition for singly-linked list. * public cla ...
- leetcode 147. Insertion Sort List ----- java
Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...
- [LeetCode] 147. Insertion Sort List 解题思路
Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...
- 【leetcode】147. Insertion Sort List
Sort a linked list using insertion sort. 链表的插入排序. 需要创建一个虚拟节点.注意点就是不要节点之间断了. class Solution { public: ...
- LeetCode 147. Insertion Sort List 链表插入排序 C++/Java
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- [leetcode sort]147. Insertion Sort List
Sort a linked list using insertion sort. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...
随机推荐
- linux 磁盘挂载操作
1. fdisk -l 查看磁盘 2. fisk /dev/vdb 进行分区 依次输入 n p 1 两次回车 wq 3. fdisk -l 查看分 ...
- Python 函数 memoryview()
memoryview() 函数返回给定参数的内存查看对象(Momory view). 所谓内存查看对象,是指对支持缓冲区协议的数据进行包装,在不需要复制对象基础上允许Python代码访问.返回元组列表 ...
- php-fpm设置与 phpMyadmin超时 操作SQL超时
LNMP 一键安装包环境: Phpmyadmin 登录超时 (1440 秒未活动),请重新登录. vim /usr/local/php/etc/php.ini session.gc_maxlife ...
- Python--线性代数篇
讲解Python在线性代数中的应用,包括: 一.矩阵创建 先导入Numpy模块,在下文中均采用np代替numpy import numpy as np 矩阵创建有两种方法,一是使用np.mat函数或者 ...
- Unit04: JSP基本语法 、 JSP运行原理
Unit04: JSP基本语法 . JSP运行原理 hello.jsp <%@page pageEncoding="utf-8"%> <!doctype html ...
- C++内存管理原则
内存可分配的地方有2个: 栈Stack和堆Heap. 内存管理的方式有3种: RAII, 智能指针或者GC. 不推荐自己管理裸指针. C++的内存管理其实是一个哲学问题: 怎样才能确定一个东西没有被其 ...
- snmpd 子代理模式编译测试
1.参考链接 1)Net-snmp添加子代理示例 https://blog.csdn.net/eyf0917/article/details/39546651 2.操作步骤 1)网络拷贝下面的文件 ...
- ubuntu64,ndk-r9 编译 ffmpeg 2.1.1的config文件
#!/bin/bash NDK_ROOT=/home/wjh/fox/android-ndk-r9c/ PREBUILT=${NDK_ROOT}toolchains/arm-linux-android ...
- ptyhon中文本挖掘精简版
import xlrd import jieba import sys import importlib import os #python内置的包,用于进行文件目录操作,我们将会用到os.listd ...
- Echarts运用
echarts客户端写法:http://echarts.baidu.com/doc/example.html ,下载echarts-2.0.4.jar包,把src里面的js引入到项目里,在放esl. ...