LeetCode147:Insertion Sort List
题目:
Sort a linked list using insertion sort.
解题思路:
按题目要求,直接进行插入排序
实现代码:
#include <iostream> using namespace std;
/*
Sort a linked list using insertion sort.
*/
struct ListNode
{
int val;
ListNode *next;
ListNode(int x):val(x), next(NULL){}
}; void addNode(ListNode* &head, int val)
{
ListNode *newNode = new ListNode(val);
if(head == NULL)
{
head = newNode;
}
else
{
newNode->next = head;
head = newNode;
}
} void PrintList(ListNode *root)
{
ListNode *head = root;
while(head != NULL)
{
cout<<head->val<<"\t";
head = head->next;
}
cout<<endl;
} class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(head == NULL || head->next == NULL)
return head;
ListNode *p = head->next;//保存除第一个节点之后的所有节点,待之后逐个进行插入排序
head->next = NULL;
while(p)
{
ListNode *t = p->next;//保存p节点的下一个节点
if(p->val < head->val)//如果当前要插入的节点小于头节点
{ p->next = head;
head = p;//将头结点指向新插入的节点
}
else//否则从头结点的下一个节点开始逐个与当前要插入节点比较,直到遇到大于当前节点的节点
{
ListNode *q = head;
while(q->next && q->next->val <= p->val)
q = q->next; p->next = q->next;
q->next = p;
}
p = t;
}
return head;
}
}; int main(void)
{
ListNode *head = new ListNode();
addNode(head, );
addNode(head, );
addNode(head, );
PrintList(head); Solution solution;
head = solution.insertionSortList(head);
PrintList(head); return ;
}
LeetCode147:Insertion Sort List的更多相关文章
- Leetcode147. Insertion Sort List对链表进行插入排序
对链表进行插入排序. 从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示). 每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中. 插入排序算法: 插入排序是 ...
- [Swift]LeetCode147. 对链表进行插入排序 | Insertion Sort List
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- 经典排序算法 – 插入排序Insertion sort
经典排序算法 – 插入排序Insertion sort 插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...
- 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 ...
随机推荐
- mogodb查询
Find: {$and: [ {"MethodName":"CommLogin"} ,{"CreateTime":{$gte:"2 ...
- .net为图片添加水印(转) jpg png和gif格式
.net为图片添加水印(转) jpg png和gif格式 .net为图片添加水印(转) jpg png和gif格式,转自csdn的hyde82,现在跟大家一起来分享下: 利 用.net中System. ...
- OC - 缓存 - NSCache - 介绍
- [leetcode]115. Distinct Subsequences 计算不同子序列个数
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- jQuery之双下拉框
双下拉框要实现的效果,实际上就是左边下拉选择框里的内容,可以添加到右边,而右边同理.写了个简单的例子,来说明一下. 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- Html使用Iframe无刷新上传文件,后台接收
html代码:我是发送请求到teacher_center.aspx,不是到.ashx一般处理程序里,需要加 runat="server",有空我再试试发送请求到 .ashx 里 & ...
- Codeforces 608B. Hamming Distance Sum 模拟
B. Hamming Distance Sum time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- Java中通过SimpleDateFormat格式化当前时间:/** 输出格式:20060101010101001**/
import java.util.*; import java.text.SimpleDateFormat; int y,m,d,h,mi,s,ms; String cur; Calendar cal ...
- 外网不能访问阿里云服务器的apache服务
今天弄了下Ubuntu的服务器,配置了一个LAMP环境的服务器,配置有时间我再来说 但是配置了很长的时间,差不都怕是好几个小时 在配置apache的时候,出现了问题,好不容易把apache服务配置好 ...
- 2018.10.16 NOIP模拟 华莱士(并查集)
传送门 按照题意模拟维护最小的环套树森林就行了. 然而考试的时候naivenaivenaive瞎写了一个错误的贪心. 代码