leetcode解題之23.Merge k Sorted Lists Java版本(合併k個有序的連結串列)
23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
合併k個有序的連結串列
public ListNode mergeKLists(ListNode[] lists) { if (lists == null || lists.length == 0) return null; return MSort(lists, 0, lists.length - 1); } public ListNode MSort(ListNode[] lists, int low, int high) { if (low < high) { int mid = (low + high) / 2; ListNode leftlist = MSort(lists, low, mid); ListNode rightlist = MSort(lists, mid + 1, high); return mergeTwoLists(leftlist, rightlist); } // 如果相等,只有一個元素,返回即可 return lists[low]; } // 遞迴合併連結串列 public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode res = null; if (l1 == null) return l2; if (l2 == null) return l1; if (l1.val <= l2.val) { res = l1; l1.next = mergeTwoLists(l1.next, l2); } else { res = l2; l2.next = mergeTwoLists(l1, l2.next); } return res; }
使用小頂堆
public ListNode mergeKLists(ListNode[] lists) { if (lists == null || lists.length == 0) return null; // PriorityQueue 是堆,預設小頂堆 PriorityQueue<ListNode> min = new PriorityQueue<ListNode>(11, new Comparator<ListNode>() { @Override public int compare(ListNode o1, ListNode o2) { return o1.val - o2.val; } }); // 加入所有連結串列的第一個結點,非空 for (ListNode node : lists) if (node != null) min.offer(node); ListNode head = new ListNode(0); ListNode cur = head; while (!min.isEmpty()) { ListNode temp = min.poll(); cur.next = temp; cur = cur.next; // 邊取邊加入 if (temp.next != null) min.offer(temp.next); } // 注意斷鏈 cur.next = null; return head.next; }
相關推薦
leetcode解題之23.Merge k Sorted Lists Java版本(合併k個有序的連結串列)
23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze a
leetcode解題之21# Merge Two Sorted Lists Java版 遞迴和非遞迴實現
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new li
LeetCode 21: Merge Two Sorted Lists(合併兩個有序連結串列)
原題 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the fir
leetcode 21 Merge Two Sorted Lists (合併兩個有序連結串列)
題目要求 合併兩個已排序的連結串列並將其作為新列表返回。 新連結串列應該通過拼接前兩個連結串列的節點來完成。 例子 Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4
LeetCode:Merge Two Sorted Lists(合併兩個有序連結串列)
題目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the fi
Merge Two Sorted Lists(合併兩個有序連結串列)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing to
21 合併兩個有序連結串列 merge two sorted lists &&23 合併k個有序連結串列 merge k sorted lists
1. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; }
21 合併兩個有序連結串列 merge two sorted lists &&23 合併k個有序連結串列 merge k sorted lists
1. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) {
C#LeetCode刷題之#21-合併兩個有序連結串列(Merge Two Sorted Lists)
問題 將兩個有序連結串列合併為一個新的有序連結串列並返回。新連結串列是通過拼接給定的兩個連結串列的所有節點組成的。 輸入:1->2->4, 1->3->4 輸出:1->1->2->3->4->4 Merge
LeetCode 21. 合併兩個有序連結串列(Merge Two Sorted Lists)
題目描述 將兩個有序連結串列合併為一個新的有序連結串列並返回。新連結串列是通過拼接給定的兩個連結串列的所有節點組成的。 示例: 輸入:1->2->4, 1->3->4 輸出:1-
LeetCode 21. 合併兩個有序連結串列 Merge Two Sorted Lists(C語言)
題目描述: 將兩個有序連結串列合併為一個新的有序連結串列並返回。新連結串列是通過拼接給定的兩個連結串列的所有節點組成的。 示例: 輸入:1->2->4, 1->3->4 輸出:1->1->2->3->4->4
leetcode-21.Merge Two Sorted Lists 合併兩個有序連結串列
題目: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first
合併兩個有序連結串列(LeetCode 21. Merge Two Sorted Lists)
雙指標連結串列解法: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x)
leetcode_效率題解_23. Merge k Sorted Lists(合併k個有序連結串列)
題目連結 【題目】 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its
[LeetCode]21. Merge Two Sorted Lists合併兩個有序連結串列
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: In
LeetCode 21. Merge Two Sorted Lists 合併兩個有序連結串列
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), nex
LeetCode 21:合併兩個有序連結串列 Merge Two Sorted Lists
將兩個有序連結串列合併為一個新的有序連結串列並返回。新連結串列是通過拼接給定的兩個連結串列的所有節點組成的。 Merge two
LeetCode#21-Merge Two Sorted Lists-合併兩個有序連結串列
#### 一、題目 將兩個升序連結串列合併為一個新的升序連結串列並返回。新連結串列是通過拼接給定的兩個連結串列的所有節點組成的。 示例: ``` 輸入:1->2->4, 1->3->4 輸出:1->1->2->3->4->4 ``` #### 二、題解 - 解法1:遞迴 終止條件:兩條連結串列分別為
21. 合併兩個有序連結串列 Merge Two Sorted Lists
文章目錄 題目:合併兩個有序連結串列 Merge Two Sorted Lists 參考答案 遇到的問題 題目:合併兩個有序連結串列 Merge Two Sorted Lists 將兩個有序連結串列合併為一個新的有序連結串列並返回。新連
leetcode解題之 11. Container With Most Water Java版(最大盛水容積)
11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represen