题解 | #单链表的排序#

单链表的排序

https://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08

import java.util.*;
public class Solution { 
    //合并两段有序链表 fast-template
    ListNode merge(ListNode pHead1, ListNode pHead2) {
        //一个已经为空了,直接返回另一个
        if(pHead1 == null)
            return pHead2;
        if(pHead2 == null)
            return pHead1;
         //加一个表头
        ListNode head = new ListNode(0);
        ListNode cur = head;
        //两个链表都要不为空
        while(pHead1 != null && pHead2 != null){
            //取较小值的结点
            if(pHead1.val <= pHead2.val){
                cur.next = pHead1;
                //只移动取值的指针
                pHead1 = pHead1.next;
            }else{
                cur.next = pHead2;
                //只移动取值的指针
                pHead2 = pHead2.next;
            }
             //指针后移
            cur = cur.next;
        }
        //哪个链表还有剩,直接连在后面
        if(pHead1 != null)
            cur.next = pHead1;
        else
            cur.next = pHead2;
        //返回值去掉表头
        return head.next;
    }
    public ListNode sortInList (ListNode head) { 
        //链表为空或者只有一个元素,直接就是有序的
        if(head == null || head.next == null)
            return head;
        ListNode left = head;
        ListNode mid = head.next;
        ListNode right = head.next.next;
        //右边的指针到达末尾时,中间的指针指向该段链表的中间
        while(right != null && right.next != null){
            left = left.next;
            mid = mid.next;
            right = right.next.next;
        }
        //左边指针指向左段的左右一个节点,从这里断开
        left.next = null;
        //分成两段排序,合并排好序的两段
        return merge(sortInList(head), sortInList(mid));
    }
}

全部评论
归并排序思想 两有序链表排序+归并 注意找到中间节点并断开 left=head mid=head.next right=head.next.next left mid移动一格 right移动两个 mid指向第二个链表头
点赞 回复 分享
发布于 2024-01-06 01:00 吉林

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务