改进 0143 重排链表

在使用数组存储链表节点的写法中,最后再判断数组长度为偶数时时没有必要的,因为在循环体while里面,我们用的是 i<=j。
This commit is contained in:
Luo
2021-10-15 20:16:39 +08:00
committed by GitHub
parent f88678d3da
commit 77d346988b

View File

@ -50,10 +50,6 @@ public:
cur = cur->next; cur = cur->next;
count++; count++;
} }
if (vec.size() % 2 == 0) { // 如果是偶数,还要多处理中间的一个
cur->next = vec[i];
cur = cur->next;
}
cur->next = nullptr; // 注意结尾 cur->next = nullptr; // 注意结尾
} }
}; };
@ -249,12 +245,6 @@ public class ReorderList {
cur = cur.next; cur = cur.next;
count++; count++;
} }
// 当是偶数的话,需要做额外处理
if (list.size() % 2== 0){
cur.next = list.get(l);
cur = cur.next;
}
// 注意结尾要结束一波 // 注意结尾要结束一波
cur.next = null; cur.next = null;
} }
@ -376,11 +366,6 @@ var reorderList = function(head, s = [], tmp) {
cur = cur.next; cur = cur.next;
count++; count++;
} }
// 当是偶数的话,需要做额外处理
if(list.length % 2 == 0){
cur.next = list[l];
cur = cur.next;
}
// 注意结尾要结束一波 // 注意结尾要结束一波
cur.next = null; cur.next = null;
} }