LeetCode【148.排序链表】

本文介绍了一种在O(nlog(n))时间复杂度和常数级空间复杂度下对链表进行排序的方法,采用自顶向下的归并排序策略,通过快慢指针将链表分割成两半,然后递归地对每一半进行排序,最后合并两个有序链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

O(nlog(n))O(nlog(n))O(nlog(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) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode slow = head, fast = head, prev = null;
        while(fast != null && fast.next != null) {
            prev = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        prev.next = null;
        ListNode l1 = sortList(head);
        ListNode l2 = sortList(slow);
        return Merge(l1, l2);
        
    }
    public ListNode Merge(ListNode l1, ListNode l2) {
        ListNode l = new ListNode(-1);
        ListNode dummpy = l;
        while(l1 != null && l2 != null) {
            if(l1.val < l2.val) {
                l.next = l1;
                l1 = l1.next;
            }else{
                l.next = l2;
                l2 = l2.next;
            }
            l = l.next;
        }
        if(l1 != null) {
            l.next = l1;
        }
        if(l2 != null) {
            l.next = l2;
        }
        return dummpy.next;
    }
}

复杂度分析

  • 时间复杂度O(nlog(n))O(nlog(n))O(nlog(n))
  • 空间复杂度O(n)O(n)O(n)
    完整代码
package leetcode148;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by 张超帅 on 2018/10/19.
 */
class ListNode {
    int val;
    ListNode next;
    ListNode(int x){this.val = x;}
}
class Solution {
    public ListNode sortList(ListNode head){
        if(head == null || head.next == null) {
            return head;
        }
        ListNode slow = head, fast = head, prev = null;

        while(fast != null && fast.next != null) {
            prev = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        prev.next = null;
        ListNode l1 = sortList(head);
        ListNode l2 = sortList(slow);
        return Merge(l1, l2);
    }
    public ListNode Merge(ListNode l1, ListNode l2){
        ListNode l = new ListNode(-1);
        ListNode cur = l;
        while(l1 != null && l2 != null){
            if(l1.val < l2.val){
                cur.next = l1;
                l1 = l1.next;
            }else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        if(l1 != null) {
            cur.next = l1;
        }
        if(l2 != null) {
            cur.next = l2;
        }
        return l.next;
    }
}
public class leetcode148 {
    public static int[] stringToArrays(String input){
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if(input == null) {
            return new int[0];
        }
        String[] parts = input.split(",");
        int[] res = new int[parts.length];
        for(int i = 0; i < parts.length; i ++) {
            res[i] = Integer.parseInt(parts[i]);
        }
        return res;
    }
    public static ListNode stringToListNode(String input) {
        int[] nodes = stringToArrays( input);
        ListNode dummpy = new ListNode(-1);
        ListNode cur = dummpy;
        for(int node : nodes){
            cur.next = new ListNode(node);
            cur = cur.next;
        }
        return dummpy.next;

    }
    public static String listnodeToString(ListNode head){
        if(head == null){
            return "[]";
        }
        String res = "";
        while(head != null){
            res += head.val + ", ";
            head = head.next;
        }
        return "[" + res.substring(0, res.length() - 2) + "]";
    }
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while((line = in.readLine()) != null) {
            ListNode head = stringToListNode(line);
            ListNode ret = new Solution().sortList(head);
            System.out.println(listnodeToString(ret));
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值