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的更多相关文章

  1. [LeetCode] Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...

  2. 经典排序算法 – 插入排序Insertion sort

    经典排序算法 – 插入排序Insertion sort  插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...

  3. leetcode Insertion Sort List

    题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...

  4. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  5. 9. Sort List && Insertion Sort List (链表排序总结)

    Sort List Sort a linked list in O(n log n) time using constant space complexity.                   H ...

  6. LeetCode OJ 147. Insertion Sort List

    Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...

  7. Java for LeetCode 147 Insertion Sort List

    Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...

  8. 【LeetCode OJ】Insertion Sort List

    Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...

  9. 147. Insertion Sort List

    Sort a linked list using insertion sort. 代码如下: /** * Definition for singly-linked list. * public cla ...

  10. leetcode 147. Insertion Sort List ----- java

    Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...

随机推荐

  1. UESTC_敢说就敢做 CDOJ 631

    敢说就敢做 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...

  2. Integer to English Words 解答

    Question Convert a non-negative integer to its english words representation. Given input is guarante ...

  3. softlayerFastUploadVHDtoBS

    Object Storage Uploader Overview We’ve recently added the option to import customer-supplied Virtual ...

  4. WinForm 控件不闪烁

    1: [DllImport("user32")] 2: public static extern int SendMessage(IntPtr hwnd, int wMsg, in ...

  5. 无心插柳OR志在必得?阿里推“来往”的意图

        近年来,阿里巴巴在外围的动作确实不少,投资新浪微博.投资陌陌,配合阿里自身的一些战略调整,让人觉得这家公司似乎正在经历一场前所未有的“蜕变”.其实这也不难理解,在BAT三国演义中,任何一方都不 ...

  6. Python 获取Facebook用户Friends的爱好类别中的Top10

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: f ...

  7. 涂抹Oracle笔记1-创建数据库及配置监听程序

    一.安装ORACLE数据库软件及创建实例OLTP:online transaction processing 指那些短事务,高并发,读写频繁的数据库系统.--DB_BLOCK_SIZE通常设置较小.O ...

  8. overflow:hidden

    原来以为overflow:hidden能隐藏所有溢出的子元素,但今天发现我错了. 对于overflow:hidden的最大误解时:当一个具有高度和宽度中至少一项的容器应用了overflow:hidde ...

  9. IBM SPSS Modeler 预测建模基础(一)

    1.搜索下载IBM SPSS Modeler 14.1 32位 及 IBM SPSS Modeler 14.1 注册文件(破解布丁): 2.下载train.csv 及 test.csv: train. ...

  10. StringList 自定义快速排序

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...