BM2 链表内指定区间反转
2023/4/6小于 1 分钟
BM2 链表内指定区间反转
题目链接
题目描述
刷题思路
代码实现
export class ListNode {
constructor(val, next) {
this.val = (val == null ? 0 : val)
this.next = (next == null ? null : next)
}
}
export function reverseBetween(head, m, n) {
let start = head
let pre
// 找到起点
while (--m > 0) {
pre = start
start = start.next
}
// 找到终点
let end = head
while (--n > 0) {
end = end.next
}
// 翻转中间部分
let last = end.next
end.next = null
while (start != null) {
const next = start.next
start.next = last
last = start
start = next
}
// 将起点前的节点与终点相连
if (pre == null)
return end
pre.next = end
return head
}
一些建议
更新日志
2024/11/1 09:51
查看所有更新日志
ccf4c
-于5a2b2
-于c0f2d
-于06596
-于9b9e4
-于c374b
-于b0275
-于5f1e1
-于2b8a3
-于03b38
-于02ab1
-于871d0
-于2a2f2
-于