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语言中常用的文件和文件夹操作函数
package main; import ( "os" "log" "time" "fmt" ) //一些常用的文件操作 ...
- Ant.OutputIsUnreadableCode
Ant在Mac OS X终端中的输出乱码的问题 1. 问题: 在用Ant脚本进行构建Android App时,在编译失败时,Ant 输出有乱码. 2. 环境: Mac OS X, 简体中文版.在Ter ...
- Delphi--最强大的开发工具(欢迎转载)
最强大的开发工具 Delphi 目录 --------------------------------------------------------------------------- 前言 De ...
- Moving Average from Data Stream LT346
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- Permutation Sequence LT60
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- 如何用xx-net上youtube
1.下载https://github.com/XX-net/XX-Net/blob/master/code/default/download.md 里面的稳定版本 2.下载chrome.百度chr ...
- Elastix GOIP 网关配合
方案一 Gateway disallow=allallow=alaw&ulawcanreinvite=nodtmfmode=rfc2833host=192.168.1.108insecure= ...
- Google Reader 快关了!!
现在还每天用Google Reader, 每次打开都提示7月1号要关闭... 上图怀念: 控制区功能:排序.展开\收缩显示.上一条\下一条,还有下拉框下的很多功能... 列表显示 针对每个Item下的 ...
- codeforces题目合集(持续更新中)
CF280CCF280CCF280C 期望dp CF364DCF364DCF364D 随机化算法 CF438DCF438DCF438D 线段树 CF948CCF948CCF948C 堆 CF961EC ...
- 2018.06.26「TJOI2018」数学计算(线段树)
描述 小豆现在有一个数 xxx ,初始值为 111 . 小豆有 QQQ 次操作,操作有两种类型: 111 $ m$ : x=x×mx=x×mx=x×m ,输出 xxx modmodmod MMM : ...