java 合并 k 个已排序的链表

牛客题目链接

1. 题目考点

  1. 归并排序
  2. merge 函数三种形式
  3. PriorityQueue

2. 考点解析

  1. 使用 ArrayList,排序再插入 (投机取巧,面试千万不可)
public ListNode mergeKLists(ArrayList<ListNode> lists) {
    ArrayList<Integer> arr = new ArrayList<>();
    
    for (int i = 0 ; i < lists.size(); i++) {
        ListNode p = lists.get(i);
        while (p != null) {
            arr.add(p.val);
            p = p.next;
        }
    }
    
    arr.sort((a,b)-> a-b);
    
    ListNode dummy = new ListNode(0), r = dummy;
    for (int i = 0; i < arr.size(); i++) {
        ListNode node = new ListNode(arr.get(i));
        r.next = node;
        r = node;
    }
    r.next = null;
    
    return dummy.next;
    
}
  1. 使用小顶堆 PriorityQueue,其实类似第一种方式 (面试可行)
public ListNode mergeKLists(ArrayList<ListNode> lists) {
    if (lists == null || lists.size() == 0) return null;
    
    Queue<Integer> pq  = new PriorityQueue<>();
    for (ListNode node : lists) {
        while (node != null) {
            pq.offer(node.val);
            node = node.next;
        }
    }
    ListNode dummy = new ListNode(0);
    ListNode r = dummy;
    while (!pq.isEmpty()) {
        ListNode node = new ListNode(pq.poll());
        r.next = node;
        r = node;
    }
    return dummy.next;
}
  1. 归并排序,先递归划分,在两两合并
public ListNode mergeKLists(ArrayList<ListNode> lists) {
    if (lists.size() == 0) return null;
    return mergeSort(lists, 0, lists.size() - 1);
}

public ListNode mergeSort(ArrayList<ListNode> lists, int l, int r) {
    if (l == r) return lists.get(l);
    int mid = l + ((r - l) >> 2);
    ListNode l1 = mergeSort(lists, l, mid);
    ListNode l2 = mergeSort(lists, mid + 1, r);
    return merge(l1, l2);
}
// 合并方式有三种,此处采用第一种 while (true) 的方式
public ListNode merge(ListNode l1, ListNode l2) {
    ListNode dummy = new ListNode(0);
    ListNode r = dummy;
    while (true) {
        if (l1 == null) {
            r.next = l2;
            break;
        } else if (l2 == null) {
            r.next = l1;
            break;
        } else if (l1.val < l2.val) {
            r.next = l1;
            l1 = l1.next;
            r = r.next;
        } else {
            r.next = l2;
            l2 = l2.next;
            r = r.next;
        }
    }
    return dummy.next;
}
  1. merge 函数采用一般方式
public ListNode merge(ListNode l1, ListNode l2) {
    ListNode dummy = new ListNode(0);
    ListNode r = dummy;
    while (l1 != null && l2 != null) {
        if (l1.val < l2.val) {
            r.next = l1;
            l1 = l1.next;
        } else {
            r.next = l2;
            l2 = l2.next;
        }
        r = r.next;
    }
    if (l1 !=  null) r.next = l1;
    if (l2 !=  null) r.next = l2;
    return dummy.next;
}
  1. merge 函数采用递归方式(不容易想到,本质是修改链)
public ListNode merge(ListNode l1, ListNode l2) {
    if (l1 == null) return l2;
    if (l2 == null) return l1;
     
    if (l1.val < l2.val) {
        l1.next = merge(l1.next, l2);
        return l1;
    }
    else {
        l2.next = merge(l1, l2.next);
        return l2;
    }
}
要实现合并K个升序链表,我们可以考虑使用分治法,将K个链表划分为两个子问题,分别合并这两个子问题,然后不断递归下去。 具体实现过程如下: 1. 将K个链表按照长度平均划分为两个子问题,每个子问题递归调用合并函数,直到只剩下一个链表或两个链表。 2. 合并两个链表的过程可以使用归并排序中的合并函数,将两个链表合并为一个升序链表。 3. 将合并后的链表返回,然后递归回去继续合并两个子问题的结果。 Java代码实现如下: ``` public ListNode mergeKLists(ListNode[] lists) { if (lists == null || lists.length == 0) { return null; } return mergeKLists(lists, 0, lists.length - 1); } private ListNode mergeKLists(ListNode[] lists, int left, int right) { if (left == right) { return lists[left]; } int mid = (left + right) / 2; ListNode l1 = mergeKLists(lists, left, mid); ListNode l2 = mergeKLists(lists, mid + 1, right); return mergeTwoLists(l1, l2); } private ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } if (l2 == null) { return l1; } if (l1.val < l2.val) { l1.next = mergeTwoLists(l1.next, l2); return l1; } else { l2.next = mergeTwoLists(l1, l2.next); return l2; } } ``` 其中,`mergeKLists` 函数是递归调用的入口函数,它接收一个 `ListNode` 数组作为参数,表示要合并的K个链表。在函数中,我们首先判断链表数组是否为空或长度为0,如果是,则返回 `null`。否则,我们调用 `mergeKLists` 函数,将链表数组划分为两个子问题,然后递归调用 `mergeKLists` 函数,继续划分子问题,直到只剩下一个链表或两个链表。 在 `mergeKLists` 函数中,我们使用归并排序的思想,将两个链表合并为一个升序链表。具体实现是在 `mergeTwoLists` 函数中,它接收两个链表作为参数,递归调用自身,将两个链表合并为一个升序链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值