LeetCode Insertion Sort List
来源:互联网 发布:it auditing 编辑:程序博客网 时间:2024/05/08 00:05
LeetCode : Insertion Sort List
Insertion Sort List
Sort a linked list using insertion sort.
此题目的实现方式有很多,依照要求,我就写了插入排序吧。需要注意几点:
1、此题的head节点也是有val的。并不是不带数值的纯粹头结点。。这点题目没说明,需要注意
2、插入时候注意位置是不是头结点之前,即:是否有节点的数值比头结点数值更小
ListNode *insertionSortList(ListNode *head) {if(NULL == head){return NULL;}ListNode *p = head;ListNode *q = p->next;while(q!=NULL){if(q->val < p->val){ListNode* pFront = head;ListNode *pTmp = head;//查找q应该插入的位置while(pTmp->val < q->val){pFront = pTmp;pTmp = pTmp->next;}<span style="white-space:pre"></span>//插入的位置在头结点之前if(pTmp==head){p->next = q->next;q->next = head;head = q;q = p->next;}else{p->next = q->next;q->next = pFront->next;pFront->next = q;q = p->next;}}else{//p,q指针后移p = q;q = q->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
- 在枚举中使用位移
- HitS内部的缓存实现源代码
- unity的Mecanim动画系统基础教程
- Android:ListView组件的优化
- 纯代码定义类似新浪微博的UItableViewCell
- LeetCode Insertion Sort List
- 如何订制个性化的网址导航
- redis优化配置和redis.conf说明
- Android监听SMS发送状态并获取短信服务中心号码
- 使用boost::circular_buffer_space_optimized实现OTL数据库连接池
- 二叉搜索树非递归后序遍历的尝试
- 【boost学习】之boost::asio(4)——boost::asio::buffer用法
- FPGA之FIFO设计(二)
- Java模式(适配器模式)