leetcode_c++:链表:Insertion Sort List(147)
2016-07-18 14:03
591 查看
Sort a linked list using insertion sort.
O(n^2)
O(n^2)
ListNode *insertionSortList(ListNode *head) { if(head == NULL) return head; ListNode *p = new ListNode(-1); p->next = head; ListNode *pre = head; ListNode *cur = pre->next; while(cur){ if(cur->val >= pre->val){ pre = cur; cur = cur->next; }else{ ListNode *insertPre = p; ListNode *insertCur = p->next; while(insertCur->val < cur->val){ insertPre = insertCur; insertCur = insertCur->next; } pre->next = cur->next; cur->next = insertCur; insertPre->next = cur; cur = pre->next; } } head = p->next; return head; }
相关文章推荐
- c++面试题经典
- leetcode_c++:链表:Reorder List(143)
- C/C++的开发环境安装
- leetcode_c++:链表:Linked List Cycle II(142)
- leetcode_c++:链表:Linked List Cycle II(142)
- std::string用法总结
- leetcode_c++:链表:Linked List Cycle(141)
- C语言开发注意事项
- 素数环 搜索
- 23 DesignPatterns学习笔记:C++语言实现
- C++ —— 入门
- C++PP Chapter XVI String类和标准模板库
- effective c++读书笔记 条款3
- C++学习
- C++学习
- Base64Code.cpp
- 详细配置C++借助ADO连接Access数据库
- C++学习--派生&继承巧记
- C语言学习笔记(二)
- NYOJ 表达式求值问题