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

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

排序链表

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;
    }
}

猜你喜欢

转载自blog.csdn.net/xiaomagezuishuai/article/details/114187486
今日推荐