LeetCode-M-Insertion Sort List
来源:互联网 发布:mysql linux安装后在哪 编辑:程序博客网 时间:2024/05/02 00:49
题意
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* tail = head; ListNode* next = head->next; while(next != NULL){ ListNode* pre = NULL; ListNode* cur = head; while(cur != next && cur->val < next->val){ pre = cur; cur = cur->next; } if(cur == next) { tail = next; }else if(pre == NULL){ tail->next = next->next; next->next = head; head = next; }else{ tail->next = next->next; next->next = pre->next; pre->next = next; } next = tail->next; } return head; }};
0 0
- LeetCode-M-Insertion Sort List
- 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
- IP地址、网关和域等区别
- BZOJ P4205[FJ2015集训]卡牌配对
- SSL1659 栈练习括弧匹配检验
- scon入门
- 高中1029
- LeetCode-M-Insertion Sort List
- nyoj 139 我排第几个
- BZOJ 1567: [JSOI2008]Blue Mary的战役地图 Hash+set
- KMP学长讲课
- javaSE_07Java中类和对象-封装特性-思维导图
- pcDuino---通过SSH命令行上网
- 题目1034:寻找大富翁
- 项目后期Lua接入笔记09--策划Excel转Lua
- java jdbc 连接Mysql数据库