Leetcode---Insertion Sort List
2018-03-17 10:23
399 查看
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;
* }
* }
*/
* 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 newhead = new ListNode(0); newhead.next = head; ListNode p, q, r; p = head; while((p != null)&&(p.next != null)){ if(p.val <= p.next.val) p = p.next; else{ q = p.next; p.next = q.next; r = newhead; while(r.next.val <= q.val){ r = r.next; } q.next = r.next; r.next = q; } } return newhead.next; } }
相关文章推荐
- LeetCode OJ Insertion Sort List
- LeetCode: Insertion Sort List(外加对单链表类题目的思考总结)
- LeetCode | Insertion Sort List(插入法排序链表)
- Insertion Sort List--leetcode
- [leetcode]Insertion Sort List
- Insertion Sort List ---LeetCode
- [Leetcode]Insertion Sort List
- 【leetcode】Insertion Sort List
- leetcode day2 -- Sort List && Insertion Sort List
- leetcode_Insertion Sort List
- leetcode-004 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
- LeetCode Insertion Sort List