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 ...
随机推荐
- [z]vc boost安装
1.下载boost_1_43_0.zip(具体到哪里下载,自己搞定) 2.解压boost_1_43_0.zip(我的是直接解压放在F盘) 3.启动vc的Command Prompt编译生成bjam.e ...
- Linux 任务管理 && 常用指令
A.linux死机 转自:https://www.deleak.com/blog/2010/10/20/sysrq/ linux死机了怎么办? 曾经啊,对着键盘上 Print Screen/SysRq ...
- c# 策略模式 加工厂模式-对象与行为分离
计算器程序 策略模式是一种行为学模式.行为是同等级的算法 ,这些行为每个模式封装到一个类里 上端提供数据 ,下端提供算法 ,中间层context context 把上端的数据和算法 放到co ...
- org.apache.commons.net.ftp
org.apache.commons.NET.ftp Class FTPClient类FTPClient java.lang.Object Java.lang.Object继承 org.apache. ...
- DOM心得
一.自定义属性值两种方法的注意事项 1.用元素节点.属性(元素节点[属性])绑定的属性值不会出现在标签上. 2.用get/set/removeAttribut(,)等绑定的属性会出现在标签上.且两种方 ...
- poj 2492(关系并查集) 同性恋
题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...
- linux 使用笔记1
Zox's code life 人生就是不停的战斗! xxx is not in the sudoers file.This incident will be reported.的解决方法 1.切换到 ...
- linux下查看内存使用情况
基本内存术语解读 1> free -m 同样是做为缓存,buffers和cache又有啥区别呢? 于是又查了些资料,发现buffers实际应该是叫“缓冲”,其英文解释是:A buffer is ...
- BZOJ1079或洛谷2476 [SCOI2008]着色方案
一道记忆化搜索 BZOJ原题链接 洛谷原题链接 发现对于能涂木块数量一样的颜色在本质上是一样的,所以可以直接压在一个状态,而这题的数据很小,直接暴力开\(6\)维. 定义\(f[a][b][c][d] ...
- 糟糕的@@identity,SCOPE_IDENTITY ,IDENT_CURRENT
在某数据库里面,某甲用@@identity来获取最近插入的id值,当在多人环境,发生获取到null值的问题. 那么@@identity是否有存在的必要? 感觉像生个孩子,多了个指头. 有的数据库的ge ...