您的位置:首页 > 其它

leetcode--Insertion Sort List

2017-08-08 12:52 357 查看
Sort a linked list using insertion sort.

[java] view
plain copy

/** 

 * 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) return null;  

        ListNode p = new ListNode(Integer.MIN_VALUE);  

        ListNode cur = head;  

        ListNode t = p;  

        while(cur!=null){  

            ListNode n = cur.next;  

            t = p;  

            while(t.next!=null&&t.next.val<cur.val){  

                t = t.next;  

            }  

            cur.next = t.next;  

            t.next = cur;  

            cur = n;  

        }  

        return p.next;  

    }  

}  

原文链接http://blog.csdn.net/crazy__chen/article/details/46564295
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: