LeetCodeMerge k Sorted Lists

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) {

       

        int length = lists.size();

        if(length == 0)return null;

        int num = 0;

        while(num<length)

        {

           
if(lists.get(num)!=null)

               
break;

            num++;

        }

        if(num == length)return null;

        ListNode[] start = new
ListNode[length];
        int
minIndex = 0;
        int min =
Integer.MAX_VALUE;
        for(int
i=0;i<length;i++)
        {

           
start[i] = lists.get(i);

           
if(start[i]!=null&&start[i].val<min)

            {

               
minIndex = i;

               
min = start[i].val;

            }

        }

        ListNode head =
lists.get(minIndex);
       
ListNode node = head;
       
start[minIndex] = start[minIndex].next;

        while(!isAllEnd(start))

        {

           
findMin(start,node);

            node =
node.next;
        }

       

        return head;

       

       
    }

    public void findMin(ListNode[] lists,ListNode node)

    {
       
int min = Integer.MAX_VALUE;

        int minIndex = 0;

        for(int
i=0;i<lists.length;i++)
       
{
           
if(lists[i]!=null&&lists[i].val<min)

            {

               
min = lists[i].val;

               
minIndex = i;

            }

        }

        node.next = lists[minIndex];

        lists[minIndex] =
lists[minIndex].next;
    }

   
    public boolean
isAllEnd(ListNode[] lists)
    {

        int length = lists.length;

        int i = 0;

        while(i<length)

        {

           
if(lists[i] != null)

            {

               
return false;

            }

            i++;

        }

        return true;

    }
}


LeetCodeMerge k Sorted Lists,布布扣,bubuko.com

时间: 2024-10-10 05:11:09

LeetCodeMerge k Sorted Lists的相关文章

【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. 采用优先队列priority_queue 把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中 1 /** 2 * Definition for singly-linked list. 3 * stru

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. 根据k个已经排好序的链表构造一个排序的链表,采用类似归并排序的算法可以通过测试 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;

[LeetCode]23. Merge k Sorted Lists

23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 给定k个排序了的链表,合并k个链表成一个排序链表. 本程序思路: 1)首先得到K个链表的长度和存在len中 2)从K个链表中找到值最小的那个节点,把该节点添加到合并链表中 3)重复len次即可把所有节点添加到合并链表中. 注意事项: 1)K个链表中有的

Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

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. 依次拼接 复杂度 时间 O(N) 空间 O(1) 思路 该题就是简单的把两个链表的节点拼接起来,我们可以用一个Dummy头,将比较过后的节点接在这个Dummy头之后.最后

leetcode 刷题之路 93 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 将k个有序链表合并成一个有序链表. 思路,之前做过两个有序链表的合并操作,对于k个链表,我们最先想到的就是能不能转化为我们熟悉的两个链表的合并,可以我们先将k个链表分为两部分,先对这两部分完成链表的有序合并操作,再对合并得到的两个链表进行合并,这是一个递归性质的描述,采用递归很容易实现这个程序,和数组

[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_23_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. //堆可以用来解决这个问题. //C++的STL中priority_queue,优先队列就是堆结构. //时间复杂度是O(NlogK),N是总数据个数,K是List的个数. //堆可以用来解决这个问题. //

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 23. Merge k Sorted Lists (合并k个排序链表) 解题思路和方法

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:此题是由合并两个排序链表演化而来,刚开始,想法比较简单,像求最大公共前缀一样,逐一求解:但是最后超时,所以马上意识到出题方是为了使用归并和分治的方法,故重新写了代码. 代码一(超时未过): /** * Definition for singly-link