leetcode_Insertion Sort List
来源:互联网 发布:杀人软件app 编辑:程序博客网 时间:2024/06/05 11:43
描述:
Sort a linked list using insertion sort.
思路:
实现对链表的插入排序,显然,只能从头开始对链表进行插入排序了,时间复杂度O(n*n)
代码:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode insertionSortList(ListNode head) { if(head==null||head.next==null) return head; ListNode newHead=new ListNode(0); newHead.next=head; ListNode temp=null,p=head,q=null; while(p.next!=null) { if(p.val<=p.next.val) { p=p.next; }else { temp=p.next; p.next=p.next.next; q=newHead; while(q!=p) { if(temp.val<q.next.val) { temp.next=q.next; q.next=temp; break; } q=q.next; } } } return newHead.next; }}
0 0
- Leetcode_Insertion Sort List
- Leetcode_insertion-sort-list
- LeetCode_Insertion Sort List
- LeetCode_Insertion Sort List
- leetcode_Insertion Sort List
- list sort
- list sort
- Sort List
- Sort List
- list - sort
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- sublime提示插件开发
- 转载 钽电容起火、钽电容爆炸、钽电容烧了
- ubuntu14.04 安装openssh-server 报依赖错误的解决过程
- CSS学习
- Windows mysql 下忘记root密码的解决办法
- leetcode_Insertion Sort List
- 我国党员增速放缓
- 项目杂记五
- 织梦登陆后台成功后又返回登陆界面怎么回事?
- [PL-12]通讯协议解析及资料下载
- EasyUI中,使用jquery或者js动态添加元素,验证失效的解决办法
- GitHub 优秀的 Android 开源项目
- Spring MVC之@RequestBody, @ResponseBody 详解
- java 容器类大集结