我试图将List的长度减少到遍历时修改的索引。但是,为了节省空间,我试图避免使用其他列表。有可能:
lists.length
public ListNode mergeKLists(ListNode[] lists) {
int amount = 0;
while(amount != 1){
amount = 0;
if(lists.length%2 == 0){
for (int i = 0; i < lists.length; i++){
lists[amount] = merge2lists(lists[i] , lists[i + 1]);
amount++;
i++;
}
lists.subList( 0 , amount - 1); //will not work
lists.length = amount; // will not work
}
}
}
答案 0 :(得分:0)
我使用另一个保留记录长度的变量解决了这个问题。
public ListNode mergeKLists(ListNode[] lists) {
int amount = 0;
int length = lists.length;
while(amount!= 1){
amount = 0;
if(lists.length%2 == 0){
for (int i=0; i<length; i++){
lists[amount] = merge2lists(lists[i] , lists[i+1]);
amount++;
i++;
}
length = amount;
}
else{.....}
}