Skip to content

Instantly share code, notes, and snippets.

@xuzheng465
Created February 4, 2022 02:02
Show Gist options
  • Save xuzheng465/4de2fab60e59217f8deec5971b45e68a to your computer and use it in GitHub Desktop.
Save xuzheng465/4de2fab60e59217f8deec5971b45e68a to your computer and use it in GitHub Desktop.
// Leetcode 92 反转链表II
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
ListNode cur = dummy.next;
for (int i = 0; i < left-1; i++) {
pre = pre.next;
cur = cur.next;
}
for (int j = 0; j < right-left; j++) {
ListNode newHead = cur.next;
cur.next = cur.next.next;
newHead.next = pre.next;
pre.next = newHead;
}
return dummy.next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment