leetcode - [5]Insertion Sort List
Sort a linked list using insertion sort.
思路:插入排序
#include <iostream>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x): val(x), next(NULL) {}
}; class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if (!head || head->next == NULL) {
return head;
} ListNode *cur, *prev, *next, *p; cur = head->next;
head->next = NULL; while (cur) {
p = head;
prev = NULL; while ((p != NULL) && (p->val < cur->val)) {
prev = p;
p = p->next;
} next = cur->next;
if (p != NULL) {
if (prev != NULL) {
cur->next = p;
prev->next = cur;
}
else {
cur->next = p;
head = cur;
}
} else {
cur->next = NULL;
prev->next = cur;
}
cur = next;
}
return head;
}
}; int main(int argc, char *argv[]) {
ListNode *p = new ListNode();
p->next = new ListNode();
p->next->next = new ListNode(-);
p->next->next->next = new ListNode(-); Solution *solution = new Solution();
p = solution->insertionSortList(p); while (p) {
cout << p->val << endl;
p = p->next;
}
return ;
}
leetcode - [5]Insertion Sort List的更多相关文章
- [Leetcode Week16]Insertion Sort List
Insertion Sort List 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/insertion-sort-list/description/ ...
- [LeetCode] 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- Java for LeetCode 147 Insertion Sort List
Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...
- leetcode 147. Insertion Sort List ----- java
Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...
- leetcode:Insertion Sort List
Sort a linked list using insertion sort. 分析:此题要求在链表上实现插入排序. 思路:插入排序是一种O(n^2)复杂度的算法,基本想法就是每次循环找到一个元素在 ...
- [LeetCode] 147. Insertion Sort List 解题思路
Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...
- leetcode 名单 Insertion Sort List
Insertion Sort List Total Accepted: 24444 Total Submissions: 96639My Submissions Sort a linked list ...
- LeetCode 147. Insertion Sort List 链表插入排序 C++/Java
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
随机推荐
- go语言websocket使用与客户端html5调用
我们通过使用如下库创建websocket服务 go get golang.org/x/net/websocket websocket服务端的代码如下: package main; import ( & ...
- SqlServer2014导出数据库的数据字典-最新版本(字段说明也能导出)
--移动360导出数据字典 -- --快速查看表结构(比较全面的) THEN obj.name ELSE '' END AS 表名, col.colorder AS 序号 , col.name AS ...
- [z]oracle 创建job
https://www.cnblogs.com/lijiasnong/p/3382578.html alter system enable restricted session;--创建表create ...
- python中的迭代器 生成器 装饰器
什么迭代器呢?它是一个带状态的对象,他能在你调用next()方法的时候返回容器中的下一个值,任何实现了__iter__和__next__()(python2中实现next())方法的对象都是迭代器,_ ...
- 解决CentOS7-python-pip安装失败
Pip介绍 pip 是一个安装和管理 Python 包的工具,python安装包的工具有easy_install, setuptools, pip,distribute.使用这些工具都能下载并安装dj ...
- Snownlp
from snownlp import SnowNLP text='宝贝自拍很帅!!!注意休息-' s=SnowNLP(text) #分词print(s.words) #词性for tag in s. ...
- proguard-rules.pro、混淆、导jar包
前记: 买了一个<精通Android Studio>本来最想看的是关于混淆导jar包的,哪知道没有,有点小失望. 好吧,自己来. 在用Android Studio开发的时候,把minify ...
- vuejs导航条动态切换active状态
用一个数组存导航条,用v-for循环它,这样可以减少代码,二可以使用它的下标来判断高亮,三还可以获取后端的导航信息来遍历 重点是在:routerLink(index, path)函数,传入当前点击的下 ...
- Vue热更新报错(log.error('[WDS] Errors while compiling. Reload prevented.'))
log.error('[WDS] Errors while compiling. Reload prevented.');中的WDS其实是webpack-dev-serverwebpack的意思,用来 ...
- coocsCreator杂记
判断是否继承 cc.isChildClassOf = function (subclass, superclass) { 获取所有super classes CCClass.getInheritanc ...