1. 题目
2. 分析
之前也有一个类似的反转链表的题目,只不过那个是k个一组,可能有多组需要反转: juejin.cn/post/733155…
这道题目其实也可以一次遍历,找到left 和 right的指针:
然后将s进行反转链表,参考juejin.cn/post/733010…
然后将头尾相连:
但是这样子有一个问题,遍历是一次,反转又是一次,就遍历了两次,是否可以只遍历一次呢?
其实可以在遍历的过程中操作指针,进行反转,找到起点之后,每遍历到一个节点,我们都将当前节点的指针指向pre,最终再次进行首尾相连:
- 找到起点
- 右移一位
- 操作指针,反转指向pre,并循环右移
- 到达终点
- 连接头尾指针
3. 代码
public ListNode reverseBetween(ListNode head, int left, int right) {
if (left == right) {
return head;
}
ListNode tail = null;
ListNode next = null;
ListNode newHead = new ListNode(0);
ListNode pre = newHead;
newHead.next = head;
next = head.next;
int c = 1;
//find left
while (c < left) {
pre = pre.next;
head = head.next;
next = next.next;
c++;
}
ListNode finalPre = pre;
tail = head;
pre = pre.next;
head = head.next;
do {
next = head.next;
head.next = pre;
pre = head;
head = next;
c++;
} while (c < right);
finalPre.next = pre;
tail.next = head;
return newHead.next;
}