[Linked List]Insertion Sort List
Sort a linked list using insertion sort.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if(head==NULL || head->next==NULL){
return head;
}
ListNode* cur = head->next,*cur_pre=head,*cur_next=NULL;
while(cur){
cur_next = cur->next; /* 当前值小于前驱值,该节点需要重新调整 */
if(cur->val < cur_pre->val){
ListNode* insert_node_pre = NULL,*insert_node=head;
while(cur->val > insert_node->val){
insert_node_pre = insert_node;
insert_node = insert_node->next;
}
insert_node_pre ? insert_node_pre->next = cur : head = cur;
cur->next = insert_node;
cur_pre->next = cur_next;
}else{
cur_pre = cur;
} cur = cur_next;
}
return head;
}
};
[Linked List]Insertion Sort List的更多相关文章
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- leetcode Insertion Sort List
题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 9. Sort List && Insertion Sort List (链表排序总结)
Sort List Sort a linked list in O(n log n) time using constant space complexity. H ...
- 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 ...
- 【LeetCode OJ】Insertion Sort List
Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...
- 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 ...
随机推荐
- 获取UILabel上最后一个字符串的位置。获取文字长度和高度,自动换行
//行的高度. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat ...
- Ngui _CD技能特效
using UnityEngine;using System.Collections; public class Skill : MonoBehaviour { public float coldTi ...
- OpenCV——人脸检测
OpenCV支持的目标检测的方法: 利用样本的Haar特征进行的分类器训练,得到的级联boosted分类器(Cascade Classification) 1.加载级联分类器 CascadeClass ...
- M - Candy Sharing Game
Description A number of students sit in a circle facing their teacher in the center. Each student in ...
- 我的第一个QML Button的实现
编写第一个QML,在成功跑完HelloWorld后,决定自己实现Button按钮类. Button是在Quick2版本以上的QtQuick Controls出现的. 在Qt5.5.1版本中,选择插入Q ...
- poj 1149
#include <cstdio> #include <cstring> #include <queue> #define _clr(x, y) memset(x, ...
- VIM编辑器操作指令
VIM有三种操作模式: 1,命令模式--command mode 2,输入模式--insert mode 3,底行模式--last line mode [在命令模式的时候,按Shift + :出现的 ...
- activiti笔记四 关于部署信息表act_re_deployment
一.简要描述 部署流程定义时需要被持久化保存下来的信息.二.表结构说明 字段名称 字段描述 数据类型 主键 为空 取值说明 ID_ ID_ nvarchar(64) √ 主键ID NAME_ 部署名称 ...
- Android基础学习之context
Context既是环境变量,也是句柄(handler),也是上下文.类似用使用工具的工具,比如写字来说,笔是工具,Context可以看成是手,用来使用笔.context具有唯一性,具有很多种行为(定义 ...
- Python即时网络爬虫项目: 内容提取器的定义(Python2.7版本)
1. 项目背景 在Python即时网络爬虫项目启动说明中我们讨论一个数字:程序员浪费在调测内容提取规则上的时间太多了(见上图),从而我们发起了这个项目,把程序员从繁琐的调测规则中解放出来,投入到更高端 ...