[LeetCode OJ] Insertion Sort List 解题报告
来源:互联网 发布:代工上淘宝好吗 编辑:程序博客网 时间:2024/05/21 17:24
地址:https://oj.leetcode.com/problems/insertion-sort-list/
题意:链表的插入排序
注意:所有和指针有关的代码都要非常注意边界处理。
要点:理解链表和数组的插入排序的不同,插入时链表不需要向后或向前移动,比数组的插入排序要快,但复杂度还是O(N2)。
#include <iostream>#include <stack>#include <string>#include <vector>#include <cstdlib>using namespace std;/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:void insertNode(ListNode *head,ListNode *node){ ListNode *ptr = head->next; ListNode *pre = head; bool flag = 0; while(ptr != NULL){ if(ptr->val > node->val){ pre->next = node; node->next = ptr; flag = 1; break; } pre = ptr; ptr = ptr->next; } if(flag == 0){ pre->next = node; node->next = NULL; }}ListNode *insertionSortList(ListNode *head) { if(!head||!head->next) return head; ListNode L(0); ListNode *List = &L; ListNode *ptr = head; ListNode *node = NULL; while(ptr!=NULL){ node = ptr; ptr = ptr->next; insertNode(List,node); } return List->next;}};
0 0
- [LeetCode OJ] Insertion Sort List 解题报告
- LeetCode—Insertion Sort List 解题报告
- Leetcode Insertion Sort List 解题报告
- leetCode解题报告之Insertion Sort List
- Leetcode Insertion Sort List 解题报告
- 【LeetCode】Insertion Sort List 解题报告
- [leetcode] 147. Insertion Sort List 解题报告
- [Leetcode] 147. Insertion Sort List 解题报告
- [LeetCode OJ] Sort List 解题报告
- LeetCode OJ:Insertion Sort List
- [LeetCode OJ]Insertion Sort List
- LeetCode OJ Insertion Sort List
- Insertion Sort List leetcode oj
- [LeetCode]Sort List,解题报告
- LeetCode Sort List 解题报告
- Leetcode Sort List 解题报告
- LeetCode 解题报告 Sort List
- LeetCode Sort List 解题报告
- 安装numpy、nltk问题汇总
- 火车运煤问题
- 怀念旧时那一丝丝温暖的味道
- [LeetCode OJ] Sort List 解题报告
- Unique Binary Search Trees II
- [LeetCode OJ] Insertion Sort List 解题报告
- 《深入理解Java虚拟机》学习笔记——内存区域
- ResourceNotFoundException : Unable to find resource
- php二维数组排序方法(array_multisort usort)
- MAC编译4.4代码出错SyntaxError: Unable to find any JNI methods for org/chromium/ui/Clipboard.
- BASE64编解码
- 比较好的libevent简介
- jQuery中实现自定义方法的扩展
- Android 中文数字证书解释