leetcode 147 Insertion Sort List java 算法
来源:互联网 发布:阴阳师无法连接网络 编辑:程序博客网 时间:2024/06/06 05:22
题目描述:利用插入排序对链表进行排序
解体思路:修改原始链表的指针即可:
代码如下:
public static ListNode insertionSortList(ListNode head) {
if(head==null || head.next==null)
return head;
//创建一个头指针,指向原链表的第一个节点
ListNode preHead = new ListNode(-1);
//取下第一个节点,作为已排序的第一个节点
ListNode cur = head.next;
head.next = null;
ListNode p = head;
preHead.next = p;
while(cur!=null){
ListNode temp = cur.next;
ListNode pre = preHead;
p = pre.next;
boolean flag = false;
//插入排序
while(p!=null){
if(p.val>=cur.val){
pre.next = cur;
cur.next = p;
flag = true;
break;
}
else{
pre = p;
p = p.next;
}
}
if(!flag){
pre.next = cur;
cur.next = p;
}
cur = temp;
}
return preHead.next;
}
- leetcode 147 Insertion Sort List java 算法
- [Leetcode] Insertion Sort List (Java)
- LeetCode:Insertion Sort List (Java)
- Insertion Sort List Leetcode java
- LeetCode(147) Insertion Sort List
- LeetCode: Insertion Sort List [147]
- LeetCode 147 Insertion Sort List
- [leetcode 147] Insertion Sort List
- LeetCode 147 Insertion Sort List
- LeetCode 147 Insertion Sort List
- [leetcode 147]Insertion Sort List
- leetcode-147 Insertion Sort List
- [LeetCode 147]Insertion Sort List
- Leetcode[147]-Insertion Sort List
- Insertion Sort List - LeetCode 147
- leetcode[147]:Insertion Sort List
- LeetCode(147) Insertion Sort List
- leetcode 147: Insertion Sort List
- mysql主从复制一个小错误导致从库不更新数据
- 【Servlet开发】Servlet开发及其原理(一)
- 采用dlopen、dlsym和dlclose加载动态库
- IOS 数据存储——用户默认设置 NSUserDefaults
- 类别(Category)与扩展(Extension)的区分
- leetcode 147 Insertion Sort List java 算法
- OpenCV ERROR: Insufficient memory问题和imread(img_path)读取图片失败问题
- 决策树(四)--随机森林与GBDT
- 【数论】UVa OJ 113 - Power of Cryptography (密文的乘方)
- mxnet代码解析之dependency engine
- NYOJ325、NYOJ456
- 1. 使用azure powershell 管理azure redis
- Rightmost Digit
- [LeetCode] 51. N-Queens