Insertion Sort List
来源:互联网 发布:云计算超强计算能力 编辑:程序博客网 时间:2024/05/08 05:52
Sort a linked list using insertion sort.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode insertionSortList(ListNode head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(head == null || head.next == null) { return head; } ListNode dummyHead = new ListNode(Integer.MIN_VALUE); ListNode h1 = dummyHead; ListNode h2 = head; while(h2 != null) { ListNode insertionNode = findInsertionNode(h1, h2); ListNode originalPostNode = h2.next; h2.next = insertionNode.next; insertionNode.next = h2; h2 = originalPostNode; } return dummyHead.next; } private ListNode findInsertionNode(ListNode h1, ListNode h2) { ListNode pre = null; while(h1 != null && h1.val <= h2.val) { pre = h1; h1 = h1.next; } return pre; }}
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
- struts2.1.6+hibernate3.3+spring3.0遇到的问题
- Android深入浅出之Audio 第一部分 AudioTrack分析
- 关于JavaScript的面向对象和继承有利新手学习
- Java字符串——字符串的查找indexof(),lastindexOf()
- VirtualBox下实现Ubuntu共享虚拟文件夹遇到的问题
- Insertion Sort List
- linker command failed with exit code 1 (use -v to see invocation)解决方法
- C++内存管理
- 一个手机依赖症病人的反思,你们是不是这样呢
- C++面向对象类的实例题目九
- MYSQL 显示表结构
- Mysql创建、删除用户,为用户授权的相关命令
- SQL count distinct与NULL
- C++模板学习