LeeCode-Insertion Sort List
2024-10-11 01:17:04
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;
}
最新文章
- Linux测试环境部署tomcat(二)
- GDB常用命令
- VC++ 动态生成 成组的 RadioButton 按钮组
- [Intellij IDEA]File size exceeds configured limit(2560000). Code insight features are not available
- 从今天开始每天刷一题,并写在这里 分类: ACM 2015-06-16 23:52 14人阅读 评论(0) 收藏
- python写的多线程下载工具
- delete 指针
- 为什么用户主目录下.bash_profile没有自动执行
- S3C6410 纯粹的裸机启动,自己写的SD BOOT启动
- Codeforces 396B On Sum of Fractions 数论
- Android四种Activity的加载模式(转)
- jquery对象介绍及一些jquery小特效
- [bzoj1566][NOI2009]管道取珠
- 在mmdetection中跑通MaskRCNN
- NetworkStream介绍说明
- Github+HEXO FATAL bad indentation of a mapping entry at line 84
- WKWebView实现网页静态资源优先从本地加载
- 如何将Revit明细表导出为Excel文档
- vue开发 - 将方法绑定到window对象,给app端调用
- FTP中各文件目录的说明