Insertion Sort List
来源:互联网 发布:棉拖鞋淘宝 编辑:程序博客网 时间:2024/06/10 10:06
一道值得做的题,需要机智的自己定义一个头结点。
Java code:
/** * 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) { //逻辑短路 if(head == null || head.next == null){ return head; } ListNode headPtr = new ListNode(0); headPtr.next = head; ListNode curNode = head.next; ListNode sortedEnd = head; while(curNode != null){ ListNode preTmp = headPtr; ListNode tmp = headPtr.next; // Warning!! Not "ListNode tmp = head;" !! //remove deplicate value while(curNode.val > tmp.val && tmp != curNode){ preTmp = tmp; tmp = tmp.next; } if(curNode != tmp){ ListNode curNext = curNode.next; preTmp.next = curNode; curNode.next = tmp; sortedEnd.next = curNext; curNode = curNext; }else{ sortedEnd = curNode; curNode = curNode.next; } } return headPtr.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
- Debug和Release区别
- 如何安装解压版MySQL
- bytes & str
- wine安装出错
- Redis的快照功能
- Insertion Sort List
- bat
- serializeArray(),serialize(),param()用法
- 2014百度之星资格赛解题报告:Labyrinth
- apache http server 启动失败
- HTTP的工作原理
- 如何创建你自己的Git服务器
- 15.UIMenuController
- Redis的AOF功能