LeetCode 92. 反转链表 II
原题链接
简单
作者:
autumn_0
,
2025-01-02 21:08:25
,
所有人可见
,
阅读 4
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode dummy = new ListNode(0, head);
ListNode p0 = dummy;
for(int i = 0; i < left - 1; i ++ ) p0 = p0.next;
ListNode pre = null, cur = p0.next;
for(int i = 0; i < right - left + 1; i ++ ){
ListNode nxt = cur.next;
cur.next = pre;
pre = cur;
cur = nxt;
}
p0.next.next = cur;
p0.next = pre;
return dummy.next;
}
}