【Leetcode】Sort List JAVA实现

1、题:

Sort a linked list in O(n log n) time using constant space complexity.

2、分析

该题主要考查了链接上的合并排序算法。

3、代码实现

package com.edu.leetcode;
import com.edu.leetcode.ListNode;

public class SortList {

	/**
	 * @param args
	 */
	public ListNode Merge(ListNode first,ListNode second){   //合并两个有序链表,并返回新链表的投结点
		ListNode rear;
		ListNode head;
		rear=head=new ListNode(-1);
		
		while(first!=null&&second!=null){
			if(first.val<=second.val){
				rear.next=first;
				rear=first;
				first=first.next;
			}
			else{
				rear.next=second;
				rear=second;
				second=second.next;
			}
		}
		if(first!=null)
			rear.next=first;
		else
			rear.next=second;
		
		return head.next;
	}
	
	public ListNode sortList(ListNode head){     
		/*
		 * 实现链表的合并排序:1、将链表划分成基本相等的两个链表
		 * 2、递归将这两个链接继续划分,直到链表的长度为0或者1为止
		 * 3、调用Merge()将链接进行合并
		 */
		
		if(head==null||head.next==null)
			return head;
		ListNode mid =head;
		ListNode pos =mid.next;
		while(pos!=null){
			pos=pos.next;
			if(pos!=null){
				pos=pos.next;
				mid=mid.next;
			}
		}
		ListNode q=sortList(mid.next);
		mid.next=null;
		return Merge(sortList(head), q);
	}
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ListNode first1 = new ListNode(0);
		ListNode rear1 =first1;
		
		for(int i=9;i>=1;i--){
			ListNode q= new ListNode(i);
			rear1.next=q;
			rear1=q;
		}
		ListNode q=first1;
		while(q!=null){
			System.out.print(q.val+ ",");
			q=q.next;
		}
		System.out.println();
		SortList sl = new SortList();
		sl.sortList(first1);
		
		ListNode p=first1;
		while(p!=null){
			System.out.print(p.val+ ",");
			p=p.next;
		}
		System.out.println();
	}

}


4.疑问

这里的问题主要是将上面的SortList()方法分成两步实现:Divide()和

MergeSort()两部分。个人觉得应该是一样的。但运行结果确实不同,如果大神浏

览,请指点一下小弟。。


class ListNode{
	int val;
	ListNode next=null;
	public ListNode(){
	}
	public ListNode(int val){
		this.val=val;
	}
}


public class SortList {
	
	public ListNode Merge(ListNode first,ListNode second){
		ListNode rear;
		ListNode head;
		rear=head=new ListNode();
		
		while(first!=null&&second!=null){
			if(first.val<=second.val){
				rear.next=first;
				rear=first;
				first=first.next;
			}
			else{
				rear.next=second;
				rear=second;
				second=second.next;
			}
		}
		if(first!=null)
			rear.next=first;
		else
			rear.next=second;
		
		return head.next;
	}
	
	public ListNode Divide(ListNode first){   <span style="line-height: 1.5; font-family: 'Courier New'; white-space: pre-wrap;">//将一个链表划分成两个基本相等的子链表</span>

		if(first==null)
			return null;
		ListNode mid =first;
		ListNode pos =mid.next;
		while(pos!=null){
			pos=pos.next;
			if(pos!=null){
				pos=pos.next;
				mid=mid.next;
			}
		}
		ListNode q=mid.next;
		mid.next=null;
		return q;
	}
	
	public void MergeSort(ListNode first){
		if(first!=null&&first.next!=null){
			ListNode second =Divide(first);
			MergeSort(first);
			MergeSort(second);
			Merge(first, second);
		}
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ListNode first1 = new ListNode(0);
		ListNode rear1 =first1;
		
		for(int i=9;i>=1;i--){
			ListNode q= new ListNode(i);
			rear1.next=q;
			rear1=q;
		}
		
		
		
		ListNode q=first1;
		while(q!=null){
			System.out.print(q.val+ ",");
			q=q.next;
		}
		System.out.println();
		SortList sl = new SortList();
		sl.MergeSort(first1);
		
		ListNode p=first1;
		while(p!=null){
			System.out.print(p.val+ ",");
			p=p.next;
		}
		System.out.println();
		
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值