Created
February 4, 2022 02:02
-
-
Save xuzheng465/4de2fab60e59217f8deec5971b45e68a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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