首页 > 编程语言 > 详细

Java for LeetCode 147 Insertion Sort List

时间:2015-06-04 22:36:00      阅读:290      评论:0      收藏:0      [点我收藏+]

Sort a linked list using insertion sort.

解题思路:

插入排序,JAVA实现如下:

    public ListNode insertionSortList(ListNode head) {
    	if(head==null||head.next==null)
    		return head;
        ListNode root=new ListNode(Integer.MIN_VALUE);
        root.next=head;
        head=head.next;
        root.next.next=null;
        ListNode temp=root,temp2=root;
        L1:while(head!=null){
        	temp=root;
        	while(head.val>temp.next.val){
        		temp=temp.next;
        		if(temp.next==null){
        			temp.next=head;
        			head=head.next;
        			temp.next.next=null;
        			continue L1;
        		}
        	}
        	temp2=head;
        	head=head.next;
        	temp2.next=temp.next;
        	temp.next=temp2;
        }
        return root.next;
    }

 

Java for LeetCode 147 Insertion Sort List

原文:http://www.cnblogs.com/tonyluis/p/4553146.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!