Leetcode: insertion-sort-list
来源:互联网 发布:美工职业描述 编辑:程序博客网 时间:2024/06/05 14:34
题目:
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) { ListNode ans = new ListNode(Integer.MIN_VALUE); ListNode h = head; while (h != null){ ListNode p = ans; while(p.next != null && p.next.val < h.val){ p = p.next; } ListNode next = h.next; ListNode tmp = p.next; p.next = h; h.next = tmp; h = next; } return ans.next; }}
阅读全文
1 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
- bzoj1324: Exca王者之剑
- 指针与一维数组,用指针指向数组,以及指针的使用
- 解决apt-cyg安装软件出现的MD5 sum did not match, exiting错误
- 三维图形技术点
- LEETCODE 36. Valid Sudoku
- Leetcode: insertion-sort-list
- scala2
- 设计模式2
- scala编程入门(理解)
- AndroidStudio-未 commit 惨剧下的本地数据恢复
- 学习springmvc的第六天(@PathVariable 注解、Ant字符匹配、指定请求方式)
- [DP 平衡树] Codeforces 809D Round #415 (Div. 1) D. Hitchhiking in the Baltic States
- jdbc-占位符
- iOS10隐私权限