Leetcode: Sort List - 插入
来源:互联网 发布:淘宝店铺淘字号 编辑:程序博客网 时间:2024/06/07 03:43
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; head->next = NULL; ListNode *prev = NULL; ListNode *target = NULL; ListNode *tmp = NULL; while (cur != NULL) { prev = NULL; target = head; while (target != NULL && target->val <= cur->val) { prev = target; target = target->next; } if (prev == NULL) { head = cur; } else { prev->next = cur; } tmp = cur->next; cur->next = target; cur = tmp; } return head; }};
==============================第二次=========================
/** * 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 *insertion = NULL; ListNode *prev = NULL; ListNode *tmp = NULL; ListNode *cur = head->next; head->next = NULL; while (cur != NULL) { insertion = head; while (insertion != NULL && insertion->val < cur->val) { prev = insertion; insertion = insertion->next; } tmp = cur->next; if (insertion == head) { cur->next = head; head = cur; } else { prev->next = cur; cur->next = insertion; } cur = tmp; } return head; }};
0 0
- Leetcode: Sort List - 插入
- LeetCode Insertion Sort List(单链表插入排序)
- [leetcode][list][sort] Sort List
- Insertion Sort List 链表插入排序@LeetCode
- LeetCode:Insertion Sort List//链表插入排序
- leetcode Insertion Sort List(链表插入排序)
- LeetCode | Insertion Sort List(插入法排序链表)
- [C++]LeetCode: 126 Insertion Sort List (插入排序链表)
- Leetcode Insertion Sort List 插入排序链表
- insertion sort list leetcode(插入排序 链表)
- LeetCode Insertion Sort List 链表的插入排序
- LeetCode 147 Insertion Sort List(链表插入排序)
- LeetCode 147. Insertion Sort List(插入有序链表)
- leetcode 147. Insertion Sort List-链表插入排序
- 147.leetcode Insertion Sort List(medium)[链表 插入排序]
- Leetcode 147 Insertion Sort List(插入排序列表)
- [LeetCode] Insertion Sort List 单向链表插入排序
- LeetCode-147. Insertion Sort List (JAVA)链表插入排序
- 2013年周历(Oracle)
- 附加数据库失败 无法升级数据库,因为它是只读的...问题解决方式
- 电脑工作者保护自己的眼睛
- Android笔记:SurfaceView与SurfaceHolder对象
- XML教程适用于iOS:如何选择最佳的XML解析器为您的iPhone项目
- Leetcode: Sort List - 插入
- β—胡萝卜素
- Struts2.2.3.1 初次使用错误总结
- 数据库三范式
- Android ListView从网络获取图片及文字显示
- IOS开发之常用的正则表达式
- goagent GAE平台部署教程
- 二进制、八进制、十进制、十六进制之间转换
- XML Parser 概述