leetcode 23. Merge k Sorted Lists

系统管理员 2023-07-10 08:26 101阅读 0赞

目录

一、问题分析

二、代码实现

1、将第一个链表之外的所有链表分别与第一个链表合并

2、采用类似归并排序迭代版本自底向上的写法

3、小顶堆


https://leetcode.com/problems/merge-k-sorted-lists/

将k个有序链表合并成一个有序链表后返回

一、问题分析

测试用例:

  1. Example:
  2. Input:
  3. [
  4. 1->4->5,
  5. 1->3->4,
  6. 2->6
  7. ]
  8. Output: 1->1->2->3->4->4->5->6

二、代码实现

1、将第一个链表之外的所有链表分别与第一个链表合并

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10. public ListNode mergeKLists(ListNode[] lists) {
  11. if (lists == null || lists.length == 0) { //[]
  12. return null;
  13. }
  14. ListNode first = lists[0];
  15. for (int i = 1; i<lists.length; i++) {
  16. first = mergeTwoLists(first, lists[i]);
  17. }
  18. return first;
  19. }
  20. private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
  21. ListNode tempHead = new ListNode(-1);
  22. ListNode tempTail = tempHead;
  23. while (l1 != null && l2 != null) {
  24. if (l1.val <= l2.val) {
  25. tempTail.next = l1;
  26. l1 = l1.next;
  27. tempTail = tempTail.next;
  28. } else {
  29. tempTail.next = l2;
  30. l2 = l2.next;
  31. tempTail = tempTail.next;
  32. }
  33. }
  34. if (l1 != null) {
  35. tempTail.next = l1;
  36. }
  37. if (l2 != null) {
  38. tempTail.next = l2;
  39. }
  40. // return tempHead.next;
  41. ListNode temp = tempHead;
  42. tempHead = tempHead.next;
  43. temp.next = null;
  44. return tempHead;
  45. }
  46. }

2、采用类似归并排序迭代版本自底向上的写法

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10. public ListNode mergeKLists(ListNode[] lists) {
  11. if (lists == null || lists.length == 0) { //[]
  12. return null;
  13. }
  14. int step = 1; //2*step的下标跨度作为一个小组,将小组的两个链表合并
  15. while (step < lists.length) {
  16. for (int i=0; i<lists.length; i += 2*step) {
  17. if (i+step >= lists.length) {
  18. // mergeTwoLists(lists[i], null);
  19. // [[],[1]] 输出[]
  20. lists[i] = mergeTwoLists(lists[i], null);
  21. } else {
  22. // mergeTwoLists(lists[i], lists[i+step]);
  23. // [[],[1]] 输出[]
  24. lists[i] = mergeTwoLists(lists[i], lists[i+step]);
  25. }
  26. // step = 2 * step;
  27. // [[],[-1,5,11],[],[6,10]] 输出[-1,5,11]
  28. }
  29. step = step * 2;
  30. }
  31. return lists[0];
  32. }
  33. private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
  34. //[[], [1]] 输出[]
  35. if (l1 == null) {
  36. return l2;
  37. }
  38. if (l2 == null) {
  39. return l1;
  40. }
  41. ListNode tempHead = new ListNode(-1);
  42. ListNode tempTail = tempHead;
  43. while (l1 != null && l2 != null) {
  44. if (l1.val <= l2.val) {
  45. tempTail.next = l1;
  46. l1 = l1.next;
  47. tempTail = tempTail.next;
  48. } else {
  49. tempTail.next = l2;
  50. l2 = l2.next;
  51. tempTail = tempTail.next;
  52. }
  53. }
  54. if (l1 != null) {
  55. tempTail.next = l1;
  56. }
  57. if (l2 != null) {
  58. tempTail.next = l2;
  59. }
  60. // return tempHead.next;
  61. ListNode temp = tempHead;
  62. tempHead = tempHead.next;
  63. temp.next = null;
  64. return tempHead;
  65. }
  66. }

3、小顶堆

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10. public ListNode mergeKLists(ListNode[] lists) {
  11. if (lists == null || lists.length == 0) {
  12. return null;
  13. }
  14. //小顶堆
  15. Queue<ListNode> heap = new PriorityQueue(new Comparator<ListNode>(){
  16. @Override public int compare(ListNode l1, ListNode l2) {
  17. return l1.val - l2.val;
  18. }
  19. });
  20. //Queue<ListNode> heap = new PriorityQueue<>((l1, l2) -> l1.val - l2.val);
  21. //Queue<ListNode> heap = new PriorityQueue<>(lists.length, (l1,l2) -> Integer.compare(l1.val, l2.val));
  22. ListNode head = new ListNode(0), tail = head;
  23. for (ListNode node : lists) {
  24. if (node != null) {
  25. heap.offer(node);
  26. }
  27. }
  28. while (!heap.isEmpty()) {
  29. tail.next = heap.poll();
  30. tail = tail.next;
  31. if (tail.next != null) {
  32. heap.offer(tail.next);
  33. }
  34. }
  35. return head.next;
  36. }
  37. }

参考:

https://leetcode.com/problems/merge-k-sorted-lists/discuss/10809/13-lines-in-Java

https://leetcode.com/problems/merge-k-sorted-lists/discuss/10528/A-java-solution-based-on-Priority-Queue

https://leetcode.com/problems/merge-k-sorted-lists/discuss/10522/My-simple-java-Solution-use-recursion

https://leetcode.com/problems/merge-k-sorted-lists/discuss/10527/Difference-between-Priority-Queue-and-Heap-and-C%2B%2B-implementation

https://leetcode.com/problems/merge-k-sorted-lists/discuss/10917/Simple-Java-solution-using-binary-searchquite-straightforward

https://leetcode.com/problems/merge-k-sorted-lists/discuss/10640/Simple-Java-Merge-Sort

发表评论

表情:
评论列表 (有 0 条评论,101人围观)

还没有评论,来说两句吧...

相关阅读