合并两个无序链表为一个有序链表

合并两个无序链表为一个有序链表

排序链表

package com.LianBiao;

import com.node.LinkNode;

public class SortLianBiao {
	public static void main(String[] args) {
		LinkNode node = construct();
		node = sortLianBiaoMethod(node);
		printList(node);
	}
	//插入排序
	public static LinkNode sortLianBiaoMethod(LinkNode first) {
		if (first==null) {
			return first;
		}
		LinkNode head = new LinkNode(-1);
		head.next = first;
		LinkNode pre, last = first;
		LinkNode cur = first.next;
		while(cur!=null) {
			pre = findPreInsert(head, cur);
			if(pre!=last) {
				last.next = cur.next;
				cur.next  = pre.next;
				pre.next = cur;
				cur = last.next;
			}else {
				last =  last.next;
				cur=cur.next;
			}
		}
		return head.next;
	}
	
	public static LinkNode findPreInsert(LinkNode head, LinkNode node) {
		LinkNode pre= head;
		LinkNode temp = head.next;
		int value = node.value;
		while(temp!=null&&temp!=node) {
			if(value<temp.value) {
				break;
			}else {
				temp=temp.next;
				pre = pre.next;
			}
		}
		return pre;
	}
}

//归并排序

package com.LianBiao;

import com.node.LinkNode;
import sun.awt.image.ImageWatched;

public class MergeSort {
    public static void main(String[] args) {
        LinkNode node = construct();
        LinkNode nodeTest= SortMethod(node);
        printList(nodeTest);
    }

    public static LinkNode SortMethod(LinkNode head){
        if(head==null||head.next==null){
            return head;
        }
        LinkNode mid = midLinkNode(head);
        if (mid==null){
            return head;
        }
        LinkNode right = mid.next;
        mid.next = null;
        System.out.println(mid.value);
        LinkNode leftHead = SortMethod(head);
        LinkNode rightHead = SortMethod(right);
        return mergeSortMethod(leftHead, rightHead);
    }

    public static LinkNode mergeSortMethod(LinkNode left, LinkNode right){
        if(left==null){
            return right;
        }
        if(right == null){
            return left;
        }
        LinkNode node = new LinkNode(-1);
        LinkNode head=node;
        while (left!=null&&right!=null){
            if(left.value<=right.value){
                node.next = left;
                left=left.next;
            }else{
                node.next = right;
                right=right.next;
            }
            node = node.next;
        }
        if(left==null){
            node.next = right;
        }
        if(right == null){
            node.next = left;
        }
        return head.next;

    }

    public static LinkNode midLinkNode(LinkNode head){
        if(head==null||head.next==null){
            return head;
        }
        LinkNode slow = head;
        LinkNode quick = head;
        while (quick.next!=null&&quick.next.next!=null){
            slow = slow.next;
            quick = quick.next.next;
        }
        return slow;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值