Microsoft leetcode(Merge K Sorted Lists)

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

There are two ways, first use ArrayList to rewrite the linkedlist array, then sort it and use a ListNode to finalize it.

public ListNode mergeKLists(ListNode[] lists) {

  ArrayList<Integer> list = new ArrayList<>();

  for (ListNode l : lists) {

    while (l != null) {

      list.add(l.val);

      l = l.next;

    }

  }

  Collections.sort(list);

  ListNode ans = new ListNode(0);

  ListNode a = ans;

  for (int i = 0; i < list.size(); i++) {

    ListNode tmp = new ListNode(list.get(i));

    a.next = tmp;

    a = a.next;

  }

  return ans.next;

}

Second is to use a priorityQueue and change its comparator function.

知识点:Priority Queue (Use tree)

Two methods to add nodes, add() & offer(). Difference : add() throws an exception, offer() return false;

Two methods to get the first node, element() & peek(). Difference : element() throws an exception, peek() return null;

Two methods to get and remove the first node, remove() & poll(). Difference : remove() throws an exception, poll() return null;

public ListNode mergeKLists(ListNode[] lists) {

  if (lists.length == 0 || lists == null) return null;

  PriorityQueue<ListNode> queue = new PriorityQueue<ListNode>(lists.length, new Comparator<ListNode>() {

    @Override

    public int Comparator(ListNode n1, ListNode n2) {

      if (n1.val < n2.val) {

        return -1;

      } else if (n1.val == n2.val) {

        return 0;

      } else {

        return 1;

      }

    }

  });

  ListNode dummy = new ListNode(0);

  ListNode tail = dummy;

  for (ListNode l : lists) {

    if (l != null) {

      queue.offer(l);

    }

  }

  while (!queue.isEmpty()) {

    tail.next = queu.poll();

    tail = tail.next;

    if (tail.next != null) {

      queue.offer(tail.next);

    }

  }
  return dummy.next;

}

原文地址:https://www.cnblogs.com/jjjiajia/p/8452456.html

时间: 2024-10-07 16:24:14

Microsoft leetcode(Merge K Sorted Lists)的相关文章

[leetcode]Merge k Sorted Lists @ Python

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路:归并k个已经排好序的链表.使用堆这一数据结构,首先将每条链表的头节点进入堆中,然后将最小的弹出,并将最小的节点这条链表的下一个节点入堆,依次类推,最终形成的链表就是归

LeetCode: Merge k Sorted Lists [022]

[题目] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [题意] 合并K个有序链表 [思路] 归并 [代码] /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), n

LeetCode: Merge k Sorted Lists 解题报告

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Show Tags 参考资料: http://blog.csdn.net/linhuanmars/article/details/19899259. SOLUTION 1: 使用分治法.左右分别递归调用Merge K sorted List,然后再使用merg

[leetcode]Merge k Sorted Lists @ Python [基础知识: heap]

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路: 归并k个已经排好序的链表, 使用堆这一数据结构. 堆,也叫做:priority queue 首先将每条链表的头节点进入堆中. 然后将最小的弹出,并将最小的节点这条链

LeetCode -- Merge k Sorted Lists (Divide and Conquer / PriorityQueue)

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 分支策略:每次归并两个已排好序的链表,直至只剩下一个链表. public class Solution { public ListNode mergeKLists(List<ListNode> lists) { //处理特殊情况  if (lists == null)             ret

LeetCode Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next =

[Leetcode] Merge k sorted lists 合并k个已排序的链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:这题最容易想到的是,(假设有k个链表)链表1.2合并,然后其结果12和3合并,以此类推,最后是123--k-1和k合并.至于两链表合并的过程见merge two sorted lists的分析.复杂度的分析见JustDoIT的博客.算法复杂度:假设每个链表的平均长度是n,则1.2合并,遍历2n个

LeetCode——Merge k Sorted Lists

Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Subscribe to see which companies asked this question. 思路:其实就是归并排序的最后一步归并操作.思想是递归分治,先把一个大问题分成2个子问题,然后对2个子问题的解进行合并.经过一次遍历就能找出已经有序的序列.就算是题目中给

LeetCode Merge k Sorted Lists 解决报告

https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走路List<ListNode>.对当中每一个链表同当前链表做一遍类似于归并排序最后一步的merge操作. 算法复杂度是O(KN) public class Solution { ListNode mergeTwoLists(ListNode list1, ListNode list2) { Lis