归并排序-链表版

/**
 * 归并排序——递归链表版
 * @author xuan
 * @create 2021-10-13 15:21
 **/
public class GuiBingPaiXuLianBiao {

    public static void main(String[] args) {
        ListNode node1 = new ListNode(9);
        ListNode node2 = new ListNode(3);
        ListNode node3 = new ListNode(5);
        node1.next = node2;
        node2.next = node3;

        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(2);
        ListNode node6 = new ListNode(6);
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;

        ListNode sortNode = sortList(node1);
        System.out.println(sortNode);
    }

    public static ListNode sortList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode secondNode = halfSplit(head);
        System.out.println("第一段:" + head);
        System.out.println("第二段:" + secondNode);
        System.out.println("下一轮——————");
        //注意这里的写法!
        return merge(sortList(head), sortList(secondNode));
    }

    /**
     * 快慢指针法,二分链表
     */
    public static ListNode halfSplit(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        //slowNode的上一个节点
        ListNode tmpNode = head;
        ListNode fastNode = head.next.next;
        ListNode slowNode = head.next;
        while (fastNode != null && fastNode.next != null) {
            //快指针一次跑2步
            fastNode = fastNode.next.next;
            tmpNode = slowNode;
            //慢指针一次跑1步
            slowNode = slowNode.next;
        }
        //tmpNode的next置空,链表一分为二
        tmpNode.next = null;
        return slowNode;
    }

    public static ListNode merge(ListNode head1, ListNode head2) {
        ListNode head = new ListNode(0);
        ListNode tmpNode = head;
        while (head1 != null && head2 != null) {
            if (head1.val <= head2.val) {
                tmpNode.next = head1;
                //head1指针后移
                head1 = head1.next;
            } else {
                tmpNode.next = head2;
                //head2指针后移
                head2 = head2.next;
            }
            //tmp指针后移
            tmpNode = tmpNode.next;
        }
        //复制剩余节点
        while (head1 != null) {
            tmpNode.next = head1;
            tmpNode = tmpNode.next;
            head1 = head1.next;
        }
        while (head2 != null) {
            tmpNode.next = head2;
            tmpNode = tmpNode.next;
            head2 = head2.next;
        }
        return head.next;
    }

    static class ListNode {

        int val;

        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append(val);
            ListNode tmpNode = this;
            while (tmpNode.next != null) {
                tmpNode = tmpNode.next;
                sb.append("->").append(tmpNode.val);
            }
            return sb.toString();
        }
    }
}

你可能感兴趣的:(归并排序-链表版)