如果输入ListArray为空,则返回空列表[删除]

时间:2017-10-14 11:44:27

标签: java list

如果输入ListArray为空,我试图返回一个空的ListNode。

ListNode类是:

public  class ListNode {
  int val;
  ListNode next;
  ListNode(int x) { val = x; }
  }

以及当输入为[]且输出应为[]时返回空Listnode的函数是:

public  ListNode mergeKLists(ListNode[] lists) {
    if(lists == null)     // this is not the case, because an empty list is there
        return null;
    if(lists.length == 0)    
        return lists[0];   //this won't work because there's nothing at the 0th index
        return null; // won't work because I want to return an empty ListNode
}

2 个答案:

答案 0 :(得分:1)

不清楚你是什么意思"清空ListNode" 。
您可以返回new Listnode(0)new ListNode()。 对于后者,您需要定义适当的构造函数:

ListNode() { }

答案 1 :(得分:1)

你可以这样做:

db.collection.aggregate([
{   
    $project: {
         email: '$email',
         number: '$number',
         boxList: '$boxDetail.boxList'
    },
    {    $unwind: '$boxList'   },
    {
         $group: {
             _id: '$_id',
             'Total Weight': {
                $sum:{
                        $max:['$boxdetail.BoxActualWeight', '$boxdetail.BoxDimWeight']
                    }
              },
              'User Email': {
                $first: '$email'
              },
              'Order Number':{
                $first: '$number'
              }
         }

    }
}])
相关问题