insertion-sort-list
来源:互联网 发布:c语言鸡兔同笼代码 编辑:程序博客网 时间:2024/06/15 02:37
容易 链表插入排序
33%
通过
用插入排序对链表排序
您在真实的面试中是否遇到过这个题?Yes
样例
标签 Expand Given 1->3->2->0->null
, return 0->1->2->3->null
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param head: The first node of linked list. * @return: The head of linked list. */ public ListNode insertionSortList(ListNode head) { ListNode dummy = new ListNode(0); while(head != null){ //插入排序,每次都从头开始找位置 ListNode node = dummy; //从头开始找,直到node.next.val大于head.val停止,这样,找到了head该插入的位置 //所有head前的node的值比head小,head后的node的值比head大 while(node.next != null && node.next.val < head.val){ node = node.next; } //定义temp,实现head的向后移动 ListNode temp = head.next; //现在已经找到了head的位置,将其插入。 //node之后的值都比head大 head.next = node.next; //node的值比head小 node.next = head; /** * head.next = node.next; * node.next = head; * 实现了head的插入 */ head = temp; } return dummy.next; }}
0 0
- Insertion Sort List | leetcode
- Leetcode: Insertion Sort List
- [LeetCode] Insertion Sort List
- Insertion Sort List
- Leetcode: Insertion Sort List
- [LeeCode] Insertion Sort List
- leetcode-Insertion Sort List
- [LeetCode]Insertion Sort List
- Insertion Sort List
- 【LeetCode】Insertion Sort List
- LeetCode | Insertion Sort List
- LeetCode - Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- [LeetCode] Insertion Sort List
- list - insertion sort - AC
- openssl移植Android使用及其相关经验分享
- Assembly.GetExportedTypes 方法获取此程序集中定义的公共类型,这些公共类型在程序集外可见。
- 回调函数定义
- Apache+tomact搭建
- Contains Duplicate II
- insertion-sort-list
- C++ MFC 中使用多线程操作实例
- jquery提交表单错误
- Spring MVC-HandlerAdapter
- IOS的iPhone5s模拟器只显示3.5寸解决方法
- android studio 开发 gradle 详解(五)
- Python strip函数
- Java程序设计——第十六周周五:数据库的连接与随机数使用
- oracle的number详解