insertion sort not quite understand
来源:互联网 发布:手工西装淘宝 编辑:程序博客网 时间:2024/06/02 00:22
/** * 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(-2147483648); //dummyHead.next = head; ListNode p1 = dummyHead, p2 = head; while(p2 != null){ ListNode pre = findInsertPlace(p1, p2); // insert into list // save orignal list next node ListNode originalNext = p2.next; p2.next = pre.next; pre.next = p2; p2 = originalNext; } return dummyHead.next; } public ListNode findInsertPlace(ListNode p1, ListNode p2){ ListNode pre = null, cur = p1; while(cur != null && cur.val <= p2.val){ pre = cur; cur = cur.next; } return pre; }}
0 0
- insertion sort not quite understand
- Insertion Sort
- Insertion Sort
- insertion sort
- insertion sort
- Insertion Sort
- insertion sort
- insertion sort
- insertion sort
- insertion sort
- Insertion Sort
- Insertion Sort
- Insertion-sort
- Insertion sort
- Insertion sort
- insertion sort
- Insertion sort
- Insertion sort
- Block简介:
- CF383A Milking cows
- 为什么要用SSH?
- [leet code] Trapping Rain Water
- 面试题:[Container the most water]容器中最大水容量
- insertion sort not quite understand
- 一道面试题 - 找最长固定unique个数的子串
- Homebrew的安装与使用
- 摘抄代码片段
- 渣降临@Palindrome Partitioning II
- cocos2dx 精灵监听触摸事件
- [leet code] Valid Parentheses
- 数据库中创建索引的准则
- 第一章:前言