LeeCode-Insertion Sort List
Sort a linked list using insertion sort.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* insertionSortList(struct ListNode* head)
{
struct ListNode *p;
p=head;
int count=;
while(p!=NULL)
{
count++;
p=p->next;
} int *array;
array=(int *)malloc(count*sizeof(int)); p=head;
int i=,j,k;
while(p!=NULL)
{
array[i]=p->val;
p=p->next;
i++;
} for(i=;i<count;i++)
{
for(j=i-;j>=;j--)
{
if(array[j]<array[i])
break;
} if(j!=i-)
{
int tmp=array[i];
for(k=i-;k>j;k--)
{
array[k+]=array[k];
}
array[k+]=tmp;
}
} i=;
struct ListNode *q;
q=head;
while(q!=NULL)
{
q->val=array[i];
q=q->next;
i++;
} return head;
}
LeeCode-Insertion Sort List的更多相关文章
- [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 ...
- 【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 ...
随机推荐
- 构建高性能WEB站点笔记二
构建高性能WEB站点笔记 因为是跳着看的,后面看到有提到啥epoll模型,那就补充下前面的知识. 第三章 服务器并发处理能力 3.2 CPU并发计算 进程 好处:cpu 时间的轮流使用.对CPU计算和 ...
- [TI DLP Buglist]data type error in illum_EnableIllumination function
I am debuging my code today, I find when my code is running, it's stop at illum_EnableIllumination() ...
- Monkey Tradition(中国剩余定理)
Monkey Tradition Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Submi ...
- [Cycle.js] Hyperscript as our alternative to template languages
Usually we use template languages like Handlebars, JSX, and Jade to create. One simple way we can cr ...
- linux 系统下配置安装 java jdk 图文流程
先查看一下系统版本,本例采用的操作系统是CentOS 6.5: 如果你是初装之后的操作系统,那么有可能wget这个组件是不存在的,所以你要安装一下它,这样才可以让你从网上down下你要的安装包: 上面 ...
- CCS v5 无法启动解决办法及Launchpad仿真器电脑无法识别解决方法
安装ccs_setup_5.1.1.00028.exe后(无论是自己装eclipse还是在原来的基础上安装eclipse的插件),ccs5的应用无法打开,错误为:An error has occurr ...
- asp.net 追加文本(追加写入记事本)
代码: string path = Server.MapPath("/Log/Log.txt"); if (File.Exists(path)) { using (StreamWr ...
- C#实现在线更新系统
先来看一下程序完成后长什么样. 这个是程序的组成部分. 主要功能是在InitializationUpdate这个类中完成的,From1主要起到调用的作用,所以重心还是在InitializationUp ...
- MFC之树控件
树控件对应的类: CTreeControl 树控件属性设置: 启用复选框:Check Boxes = True 父节点显示+-按钮:Has Button = True ; Lines At Roots ...
- StringList 自定义快速排序
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...