链表归并排序问题的解决方式

链表归并排序的三大神器:
1.merge(l1,l2)

l1和l2分别为两个有序的链表,该方法将l1和l2合并为一个新的有序链表。

    public ListNode merge(ListNode l1,ListNode l2){
     
        ListNode dummyHead = new ListNode(0);
        ListNode head = dummyHead;
        while(l1!=null&&l2!=null){
     
            if(l1.val<l2.val){
     
                dummyHead.next = l1;
                l1 = l1.next;
                dummyHead = dummyHead.next;
            }else{
     
                dummyHead.next = l2;
                l2 = l2.next;
                dummyHead = dummyHead.next;
            }
        }
        if(l1!=null){
     
            dummyHead.next = l1;
        }
        if(l2!=null){
     
            dummyHead.next = l2;
        }
        return head.next;
    }

2.cut(head,n)
该方法将链表head切掉前n个节点,返回后半部分的链表,head缩短为只剩前n个节点的链表

    public ListNode cut(ListNode head,int n){
     
        while(--n!=0&&head!=null){
     
            head = head.next;
        }
        if(head!=null){
     
            ListNode right = head.next;
            head.next = null;
            return right;
        }else{
     
            return null;
        }
    }

3.dummyHead哑链表头
临时创建的一个链表头,可以把边界情况和普通情况做统一处理。

例题:

在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。

示例 1:

输入: 4->2->1->3
输出: 1->2->3->4
示例 2:

输入: -1->5->3->4->0
输出: -1->0->3->4->5

代码:

class Solution {
     
    public ListNode sortList(ListNode head) {
     
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode tail = head;
        int length=0;
        while(head!=null){
     
            length++;
            head = head.next;
        }
        ListNode work;
        ListNode left;
        ListNode right;
        for(int step=1;step<length;step*=2){
     
            work = dummyHead.next;
            tail = dummyHead;
            while(work!=null){
     
                left = work;
                right = cut(left,step);
                work = cut(right,step);

                tail.next = merge(left,right);
                while(tail.next!=null){
     
                    tail = tail.next;
                }
            }
        }
        return dummyHead.next;
    }

    public ListNode cut(ListNode head,int n){
     
        while(--n!=0&&head!=null){
     
            head = head.next;
        }
        if(head!=null){
     
            ListNode right = head.next;
            head.next = null;
            return right;
        }else{
     
            return null;
        }
    }

    public ListNode merge(ListNode l1,ListNode l2){
     
        ListNode dummyHead = new ListNode(0);
        ListNode head = dummyHead;
        while(l1!=null&&l2!=null){
     
            if(l1.val<l2.val){
     
                dummyHead.next = l1;
                l1 = l1.next;
                dummyHead = dummyHead.next;
            }else{
     
                dummyHead.next = l2;
                l2 = l2.next;
                dummyHead = dummyHead.next;
            }
        }
        if(l1!=null){
     
            dummyHead.next = l1;
        }
        if(l2!=null){
     
            dummyHead.next = l2;
        }
        return head.next;
    }
}

你可能感兴趣的:(算法,算法)