单链表的排序

题目描述

给定一个无序单链表,实现单链表的排序(按升序排序)。

示例

输入:head = [4,2,1,3]
输出:[1,2,3,4]

解决方法一:

时间复杂度:O(nlogn)

空间复杂度:O(logn)

归并排序(递归)

  1. 首先用快慢指针找到链表的中点
  2. 对链表进行分割
  3. 对链表进行合并(合并两个有序链表)

image

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    
    
    /**
     * 
     * @param head ListNode类 the head node
     * @return ListNode类
     */
    public ListNode sortInList (ListNode head) {
    
    
        /*
        1. 用快慢指针找到链表中点
        2. 分割链表
        3. 借助两个链表合并思想,对链表进行合并
        */
        if (head == null || head.next == null) {
    
    
            return head;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while (fast != null && fast.next != null) {
    
    
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode rightNode = slow.next;
        slow.next = null;
        
        ListNode left = sortInList(head);            //分割前半段
        ListNode right = sortInList(rightNode);      //分割后半段
        
        return merge(left, right);                   //合并两个有序链表
    }
    
    public ListNode merge (ListNode phead1, ListNode phead2) {
    
    
        if (phead1 == null || phead2 == null) {
    
    
            return phead1 == null ? phead2 : phead1;
        }
        ListNode phead = new ListNode(-1);
        ListNode ptail = phead;
        ListNode l1 = phead1, l2 = phead2;
        while (l1 != null && l2 != null) {
    
    
            if (l1.val >= l2.val) {
    
    
                ptail.next = l2;
                l2 = l2.next;
            } else {
    
    
                ptail.next = l1;
                l1 = l1.next;
            }
            ptail = ptail.next;
        }
        ptail.next = (l1 == null ? l2 : l1);
        return phead.next;
    }
}

解决方法二:

时间复杂度:O(nlogn)

空间复杂度:O(1)

使用自底向上的方法实现归并排序,则可以达到 O(1)的空间复杂度。

首先求得链表的长度length,然后将链表拆分成子链表进行合并。

image

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    
    
    /**
     * 
     * @param head ListNode类 the head node
     * @return ListNode类
     */
    public ListNode sortInList (ListNode head) {
    
    
        /*
        1. 用快慢指针找到链表中点
        2. 分割链表
        3. 借助两个链表合并思想,对链表进行合并
        */
        //==>递归:空间复杂度不是O(1)
        //==>迭代
        if (head == null || head.next == null) {
    
    
            return head;
        }
        int len = 0;
        ListNode temp = head;
        while (temp != null) {
    
    
            len++;
            temp = temp.next;
        }
        ListNode phead = new ListNode(0);
        phead.next = head;
        for (int subLen = 1; subLen < len; subLen <<= 1) {
    
    //1 -> 2 -> 4 -> 8 ...
            ListNode preNode = phead, ptail = phead.next;
            while (ptail != null) {
    
    
                ListNode phead1 = ptail;
                for (int i = 1; i < subLen && ptail.next != null; i++) {
    
    
                    ptail = ptail.next;
                }
                ListNode phead2 = ptail.next;
                ptail.next = null;
                ptail = phead2;
                for (int i = 1; i < subLen && ptail != null && ptail.next != null; i++) {
    
    
                    ptail = ptail.next;
                }
                ListNode flag = null;            //记录还未处理的后大段链表
                if (ptail != null) {
    
    
                    flag = ptail.next;
                    ptail.next = null;
                }
                preNode.next = merge(phead1, phead2);
                while (preNode.next != null) {
    
    
                    preNode = preNode.next;
                }
                ptail = flag;
            }
        }
        return phead.next;
    }
    
    public ListNode merge (ListNode phead1, ListNode phead2) {
    
    
        if (phead1 == null || phead2 == null) {
    
    
            return phead1 == null ? phead2 : phead1;
        }
        ListNode phead = new ListNode(-1);
        ListNode ptail = phead;
        ListNode l1 = phead1, l2 = phead2;
        while (l1 != null && l2 != null) {
    
    
            if (l1.val >= l2.val) {
    
    
                ptail.next = l2;
                l2 = l2.next;
            } else {
    
    
                ptail.next = l1;
                l1 = l1.next;
            }
            ptail = ptail.next;
        }
        ptail.next = (l1 == null ? l2 : l1);
        return phead.next;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44723496/article/details/113094902
今日推荐