博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Linked List]Insertion Sort List
阅读量:4318 次
发布时间:2019-06-06

本文共 1207 字,大约阅读时间需要 4 分钟。

Total Accepted: 59422 Total Submissions: 213019 Difficulty: Medium

 

Sort a linked list using insertion sort.

 
 
/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* insertionSortList(ListNode* head) {        if(head==NULL || head->next==NULL){            return head;        }        ListNode* cur = head->next,*cur_pre=head,*cur_next=NULL;        while(cur){            cur_next = cur->next;                        /* 当前值小于前驱值,该节点需要重新调整 */            if(cur->val < cur_pre->val){                ListNode* insert_node_pre = NULL,*insert_node=head;                while(cur->val > insert_node->val){                    insert_node_pre = insert_node;                    insert_node = insert_node->next;                }                insert_node_pre ? insert_node_pre->next = cur : head = cur;                cur->next = insert_node;                cur_pre->next = cur_next;            }else{                cur_pre = cur;            }                        cur = cur_next;        }        return head;    }};
Next challenges:     

转载于:https://www.cnblogs.com/zengzy/p/5041689.html

你可能感兴趣的文章
笔记:Hadoop权威指南 第8章 MapReduce 的特性
查看>>
JMeter响应数据出现乱码的处理-三种解决方式
查看>>
获取设备实际宽度
查看>>
Notes on <High Performance MySQL> -- Ch3: Schema Optimization and Indexing
查看>>
Alpha冲刺(10/10)
查看>>
数组Array的API2
查看>>
为什么 Redis 重启后没有正确恢复之前的内存数据
查看>>
No qualifying bean of type available问题修复
查看>>
第四周助教心得体会
查看>>
spfile
查看>>
Team Foundation Service更新:改善了导航和项目状态速查功能
查看>>
WordPress资源站点推荐
查看>>
Python性能鸡汤
查看>>
android Manifest.xml选项
查看>>
Cookie/Session机制具体解释
查看>>
ATMEGA16 IOport相关汇总
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>