2021-01-19

Day 1.19

排序链表

题目

在这里插入图片描述

思路(1)

head节点值依次取出,放入数组a[]中,再对a[]排序,最后依次取出a[]的元素拼接成新链表

class Solution {
    public ListNode sortList(ListNode head) {
        ListNode n = new ListNode(1),h = n;
        if(head == null)
            return null;
        int a[] = new int[50000];
        int i = 0;
        while(head != null){
            a[i++] = head.val;
            head = head.next;
        }
        int b[] = new int[i];
        int s = 0;
        while(s<i){
            b[s] = a[s];
            s++;
        }
        for(int j = b.length-1; j > 0; j--){
            for(int k = 0; k < j; k++){
                if(b[k] > b[k+1]){
                    int temp = b[k];
                    b[k] = b[k+1];
                    b[k+1] = temp;
                }
            }
        }
        for(int m = 0;m < b.length;m++){
            n.next = new ListNode(b[m]);
            n = n.next;
        }
        return h.next;
    }
}

在这里插入图片描述

思路(2)(归并)

原理:

在这里插入图片描述

代码
/**
 * Definition for singly-linked list.
 * public 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 ListNode sortList(ListNode head) {
        // 归并排序
        // 递归解法
        if(head == null || head.next == null) {
            return head;
        }

        ListNode midNode = findMidNode(head);
        // 右边链表的头结点
        ListNode rightHead = midNode.next;
        // 注意:需要维护链表 --> 断开左右链表,变成两个链表
        midNode.next = null;

        // 对左边的链表进行排序
        ListNode left = sortList(head);
        // 对右边的链表进行排序
        ListNode right = sortList(rightHead);

        // 合并链表
        return merge(left,right);


    }

    public ListNode findMidNode(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode slow = head;
        ListNode fast = head.next.next;

        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        return fast != null ? slow.next : slow;
    }

    public ListNode merge(ListNode left,ListNode right) {
        ListNode dummyNode = new ListNode(0);
        ListNode current = dummyNode;
        while(left != null && right != null) {
            if(left.val < right.val) {
                current.next = left;
                left = left.next;
            }else {
                current.next = right;
                right = right.next;
            }
            current = current.next;  
        }
        current.next = left != null ? left : right;
        return dummyNode.next;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值