Leetcode: Insertion Sort List
来源:互联网 发布:金华网络教育 编辑:程序博客网 时间:2024/06/07 05:41
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) { if (head == null || head.next == null) { return head; } ListNode dummy = new ListNode(0); dummy.next = head; ListNode pre = head; ListNode curr = head.next; while (curr != null) { ListNode p1 = dummy; ListNode p2 = dummy.next; while (p2 != curr) { if (p2.val > curr.val) { pre.next = curr.next; p1.next = curr; p1.next.next = p2; curr = pre.next; break; } else { p1 = p2; p2 = p1.next; } } if (p2 == curr) { pre = curr; curr = pre.next; } } return dummy.next; }}
0 0
- Insertion Sort List | leetcode
- Leetcode: Insertion Sort List
- [LeetCode] Insertion Sort List
- Leetcode: Insertion Sort List
- leetcode-Insertion Sort List
- [LeetCode]Insertion Sort List
- 【LeetCode】Insertion Sort List
- LeetCode | Insertion Sort List
- LeetCode - Insertion Sort List
- [LeetCode] Insertion Sort List
- Insertion Sort List (LeetCode)
- [LeetCode] Insertion Sort List
- [LeetCode]Insertion Sort List
- leetcode Insertion Sort List
- Insertion Sort List -- LeetCode
- Insertion Sort List (LeetCode)
- LeetCode Insertion Sort List
- leetcode Insertion Sort List
- Codeforces Round #275 (Div. 2) C Diverse Permutation
- 捷速ocr文字识别软件巧运用 对工作压力say no
- DirectShow基础编程 最简单transform filter 编写步骤
- 创建Windows类
- 解剖 Nginx ·自动脚本篇(2)设置初始变量脚本 auto/init
- Leetcode: Insertion Sort List
- linux ps命令
- 和字符串数组
- C语言---给定年、月、日,输出其为这一年的第几天(多版本)
- ubuntu 设置系统环境变量
- HDU 1.3.3 开门人和关门人
- 偶然发现一个linux资源网站
- 朴淳 思庵 欲掩其跡
- android自动化测试6-UIAutomator黑盒测试2