leetcode-Insertion Sort List
来源:互联网 发布:mac下做windows启动盘 编辑:程序博客网 时间:2024/06/05 17:06
Sort a linked list using insertion sort.
思路:
指针p遍历;
指针last记录已有序部分的最后一个结点
指针pre记录待插入位置的前一个结点
指针q记录待插入位置的后一个结点; 当结点p->val<q->val时,插入;需注意注意插入到第一个结点前面的特殊情况
代码:
ListNode *insertionSortList(ListNode *head)
{
ListNode *p=head->next;
while(p!=NULL)
{
ListNode *q=head;
ListNode *pre=NULL;
while(q!=p)
{
if(q->val <= p->val)
{
pre=q;
q=q->next;
}
else
{
if(pre == NULL)
{
q->next=p->next;
p->next=q;
head=p;
p=q;
}
else
{
pre->next=p;
q->next=p->next;
p->next=q;
p=q;
}
break;
}
}
p=p->next;
}
return head;
}
0 0
- Insertion Sort List | leetcode
- Leetcode: Insertion Sort List
- [LeetCode] Insertion Sort List
- Leetcode: Insertion Sort List
- leetcode-Insertion Sort List
- [LeetCode]Insertion Sort List
- 【LeetCode】Insertion Sort List
- LeetCode | Insertion Sort List
- LeetCode - Insertion Sort List
- [LeetCode] Insertion Sort List
- Insertion Sort List (LeetCode)
- [LeetCode] Insertion Sort List
- [LeetCode]Insertion Sort List
- leetcode Insertion Sort List
- Insertion Sort List -- LeetCode
- Insertion Sort List (LeetCode)
- LeetCode Insertion Sort List
- leetcode Insertion Sort List
- relocation truncated to fit: R_X86_64_32
- ADO判断数据库中字段值为NULL
- 装完Ubuntu后
- aspcms2标签大全
- 九度题目1183:守形数
- leetcode-Insertion Sort List
- MyEclipse提示出错
- 转:sublime text 2 快捷键汇总
- 分享是一种美德 Great People Share Knowledge
- 【Python】怎样从网页中提取特定的字符串/行?
- String.intern(),String.subString()
- 生产环境下was不允许重启,怎么办?
- ssh2框架搭建及代码
- 【开源夏令营】PFIF公益寻人平台(一)