LeetCode148经典题目记录(链表排序:快排+递归归并+迭代归并)

package P12;
import java.util.Arrays;

public class M77Merge {
    //归并排序★★★★★★(递归+非递归)★★★★★★
    //lc148. 排序链表
    public static void main(String[] args) {
        int []arr={1,2,31,31,21,42,53,64,652,13,34,12,14,35,64,65,46,68,68};
        int []res=mergeSortttt(arr);
        System.out.println(Arrays.toString(res));
        ListNode head=new ListNode(4);
        ListNode head1=new ListNode(2);
        ListNode head2=new ListNode(1);
        ListNode head3=new ListNode(3);
        head.next=head1;
        head1.next=head2;
        head2.next=head3;
        ListNode ress=new ListNode(0);
        ress.next=Solution.sortList(head);
        while(ress!=null){
            System.out.println(ress.val);
            ress=ress.next;
        }

    }

    private 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; }
    }
    class Solution {
        public static ListNode quickSortList1(ListNode head) {//快速排序(超时)
            return quickSortLinkedList2(head)[0];
        }

        public static ListNode[] quickSortLinkedList2(ListNode head) {
            if(head == null || head.next == null) return new ListNode[]{head,head};
            //pivot为head,定义跟踪分割左右两个链表的头尾指针
            ListNode p = head.next,headSmall= new ListNode(),headBig = new ListNode(),tailSmall = headSmall, tailBig = headBig;

            //partition操作,以pivot为枢纽分割为两个链表
            while(p != null){
                if(p.val < head.val){
                    tailSmall.next = p;
                    tailSmall = tailSmall.next;
                }else{
                    tailBig.next = p;
                    tailBig = tailBig.next;
                }
                p = p.next;
            }

            //断开=pivot的排序链表,链表变为三个部分
            head.next = null;
            tailSmall.next = null;
            tailBig.next = null;

            //递归partition
            ListNode[] left = quickSortLinkedList2(headSmall.next);
            ListNode[] right = quickSortLinkedList2(headBig.next);


            //如果有=pivot的排序链表
            head.next = right[0];

            //确定排序后的头节点和尾节点
            ListNode newHead,newTail;
            if(left[0] != null) newHead = left[0];
            else newHead = head;
            if(right[1] != null) newTail = right[1];
            else newTail = head;

            //返回当前层递归排序好的链表头节点和尾节点
            return new ListNode[]{newHead,newTail};
        }
        public static ListNode sortList(ListNode head) {//迭代O(nlogn)+O(1)
            if(head==null||head.next==null){
                return head;
            }
            //先统计数量
            int num=0;
            ListNode temp=head;
            while(temp!=null){
                temp=temp.next;
                num++;
            }
            ListNode head1=new ListNode(0,head);
            for(int i=1;i=end){
            return;
        }
        int mid=(start+end)/2;
        sortMerge13(res,nums,start,mid);//位置颠倒
        sortMerge13(res,nums,mid,end);
        int a=start;
        int b=mid;
        int c=start;
        while(a=end){
            return;
        }
        int mid=(start+end)/2;
        mergeSort(dst,nums,start,mid);
        mergeSort(dst,nums,mid,end);
        int i=start;
        int j=mid;
        int k=start;
        while(i 
 

你可能感兴趣的:(Java基础,数据结构,链表,数据结构,算法)