力扣题148排序链表

给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。

进阶:

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

示例 1:

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

示例 2:

输入:head = [-1,5,3,4,0]
输出:[-1,0,3,4,5]

示例 3:

输入:head = []
输出:[]

1.自己的做法:利用一个辅助空间优先队列,可以按顺序存储链表中每个节点,然后再按序把队列中的节点取出拼接成一个新链表。

/**
 * 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){
            return null;
        }
        //用一个优先队列,进行排序
        PriorityQueue<ListNode> pq = new PriorityQueue<>(new Comparator<ListNode>(){
            public int compare(ListNode node1,ListNode node2){
                return node1.val - node2.val;
            }
        });
        //将链表中的元素按照顺序加入优先队列
        while(head != null){
            pq.offer(head);
            head = head.next;
        }

        ListNode pHead = new ListNode(0);//标兵头节点
        ListNode pNode = pHead;
        while(!pq.isEmpty()){//将优先队列中的节点按序拼接成链表
            pNode.next = pq.poll();
            pNode = pNode.next;
        }
        pNode.next = null;

        return pHead.next;
    }
}

2.进阶:对链表的排序要想到归并排序

        ①自顶向下归并排序。先不断地将链表从中间开始拆分,然后再两两合并。利用递归实现。

          两两拼接有序数组,参考力扣题21合并两个有序链表_xxyneymar的博客-CSDN博客

class Solution {
    public ListNode sortList(ListNode head) {
        return sortList(head,null);
    }

    public ListNode sortList(ListNode startNode,ListNode endNode){
        if(startNode == null){//节点不存在时
            return startNode;
        }

        if(startNode.next == endNode){//只有一个节点时(mid节点是作为后半部分的起始节点)
            startNode.next = null;
            return startNode;
        }

        ListNode slow = startNode;//慢指针
        ListNode fast = startNode;//快指针
        while(fast != endNode){
            slow = slow.next;
            fast = fast.next;
            if(fast != endNode){//快指针还没到末尾,就再走一步,相当于慢指针走一步,快指针走两步
                fast = fast.next;
            }
        }
        ListNode mid = slow;//慢指针指向的节点就是中间节点
        ListNode list1 = sortList(startNode,mid);//左子链表
        ListNode list2 = sortList(mid,endNode);//右子链表
        ListNode sort = merge(list1,list2);//按序合并左右子链表
        return sort;
    }

    public ListNode merge(ListNode head1,ListNode head2){
        ListNode head = new ListNode(0);
        ListNode pNode = head;
        ListNode pNode1 = head1;
        ListNode pNode2 = head2;

        while(pNode1 != null && pNode2 != null){
            if(pNode1.val < pNode2.val){
                pNode.next = pNode1;
                pNode1 = pNode1.next;
            }else{
                pNode.next = pNode2;
                pNode2 = pNode2.next;
            }
            pNode = pNode.next;
        }

        if(pNode1 != null){
            pNode.next = pNode1;
        }

        if(pNode2 != null){
            pNode.next = pNode2;
        }

        return head.next;
    }
}

        ②自底向上归并排序。从长度为1的子链表,拼接成长度为2的子链表,再拼接成长度为4的慢慢向上进行拼接。

class Solution {
    public ListNode sortList(ListNode head) {
        if(head == null){
            return null;
        }

        int length = 0;
        ListNode pNode = head;
        while(pNode != null){
            length++;
            pNode = pNode.next;
        }

        ListNode dummyHead = new ListNode(0,head);//创建一个标兵

        for(int subLength = 1;subLength < length;subLength <<= 1){
            ListNode prev = dummyHead;
            ListNode curr = dummyHead.next;
            while(curr != null){
                ListNode list1 = curr;
                for(int i = 1;i < subLength && curr.next != null;i++){//截取第一个子链表
                    curr = curr.next;
                }
                ListNode list2 = curr.next;
                curr.next = null;//第一个子链表的末尾指向null
                curr = list2;
                for(int i = 1;i < subLength && curr != null && curr.next != null;i++){//截取第二个子链表
                    curr = curr.next;
                }

                ListNode next = null;//表示截完两个子链表后剩余的链表
                if(curr != null){
                    next = curr.next;
                    curr.next = null;//第二个子链表的末尾只想null
                }

                ListNode merged = merge(list1,list2);//对前两个链表进行归并排序
                prev.next = merged;
                while(prev.next != null){
                    prev = prev.next;//找到末尾节点,为了之后的拼接
                }

                curr = next;//对剩余链表进行拼接
            }
        }
        return dummyHead.next;
    }

    public ListNode merge(ListNode head1,ListNode head2){
        ListNode head = new ListNode(0);
        ListNode pNode = head;
        ListNode pNode1 = head1;
        ListNode pNode2 = head2;

        while(pNode1 != null && pNode2 != null){
            if(pNode1.val < pNode2.val){
                pNode.next = pNode1;
                pNode1 = pNode1.next;
            }else{
                pNode.next = pNode2;
                pNode2 = pNode2.next;
            }
            pNode = pNode.next;
        }

        if(pNode1 != null){
            pNode.next = pNode1;
        }

        if(pNode2 != null){
            pNode.next = pNode2;
        }

        return head.next;
    }
}

题源:力扣

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值