zoukankan      html  css  js  c++  java
  • leetcode——23. 合并K个排序链表

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def mergeKLists(self, lists):
            """
            :type lists: List[ListNode]
            :rtype: ListNode
            """
            if not lists:
                return 
            #暴力法
            r=[]
            for i in range(len(lists)):
                if not lists[i]:
                    continue
                head=lists[i]
                p=head
                while p:
                    r.append(p.val)
                    p=p.next
            if not r:
                return
            r.sort()
            head=ListNode(r[0])
            p=head
            for j in range(1,len(r)):
                p.next=ListNode(r[j])
                p=p.next
            return head
    执行用时 :76 ms, 在所有 python 提交中击败了99.27%的用户
    内存消耗 :20 MB, 在所有 python 提交中击败了18.16%的用户
     
    ——2019.10.31
     

    public ListNode mergeKLists(ListNode[] lists) {
            if(lists.length == 0){
                return null;
            }else if(lists.length == 1){
                return lists[0];
            }
            ArrayList<Integer> list = new ArrayList<>();
            for(int i = 0;i<lists.length;i++){
                ListNode node = lists[i];
                while(node != null){
                    list.add(node.val);
                    node = node.next;
                }
            }
            System.out.println(list);
            int[] a = list.stream().mapToInt(Integer::valueOf).toArray();
            Arrays.sort(a);
            if(a.length == 0){
                return null;
            }
            ListNode node = new ListNode(a[0]);
            ListNode head = node;
            for(int i = 1;i<a.length;i++){
                node.next = new ListNode(a[i]);
                node = node.next;
            }
            return head;
        }

     ——2020.7.5

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    快速掌握麦肯锡的分析思维
    如何建立数据分析的思维框架
    《七周数据分析师》-万字总结
    装饰器
    迭代器&生成器
    Excel 怎样去掉单元格中的回车符号
    python面试题(一)
    Python基础5
    Python基础4
    Python enumerate() 函数
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11773559.html
Copyright © 2011-2022 走看看