type
status
date
slug
summary
tags
category
icon
password
描述
将一个节点数为 size 链表 m 位置到 n 位置之间的区间反转
要求:时间复杂度 O(n)O(n) ,空间复杂度 O(n)O(n)
例如:给出的链表为 1→2→3→4→5→NULL
m=2,n=4
返回 1→4→3→2→5→NULL
题解
假设m n 是符合的,翻转次数为n-m+1,例如m,n是2,4。共有3个节点,4-2+1 = 3
从dummy出发走m-1个node,到达left.
left的下一个节点就是翻转的起点,马上也会是翻转尾巴,记作newTail.
翻转完后
left.next = pre;
newTail.next = next
return dummy.next;