入力ListArrayが空の場合、空のListNodeを返そうとしています。入力ListArrayが空の場合は空のリストを返します。[削除]
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
}
あなたが「空ListNode」によって何を意味するかは明らかではありません。 '新しいリストノード(0)'または '新しいリストノード()'を返すことができます – c0der
ありがとう!私はあなたの答えからヒントを得ました。基本的に 'new ListNode()'を使用すると、整数が返されるというエラーが返されます。しかし、 '新しいListNode(0).next'が機能しました! – Neha
'next 'を初期化しなかったため、' new ListNode(0).next'はnullです。詳細は回答を参照してください。 – c0der