/**92. Reverse Linked List II
* @param head
* @param m
* @param n
* @return 逆转m-n之间的数字
*/
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null) {
return null;
}
ListNode ret = new ListNode(-1);
ret.next = head;
ListNode pre = ret;
ListNode cur = head;
head = pre;
int pos = 1;
while (cur != null && pos <= n) {
if (pos < m) {
pre = cur;
head = pre;
head = cur;
cur = cur.next;
} else if (pos == m){
pre = cur;
cur = cur.next;
} else {
pre.next = cur.next;
cur.next = head.next;
head.next = cur;
cur = pre.next;
}
pos += 1;
}
return ret.next;
}
[LeetCode] 92. Reverse Linked List II java
最新推荐文章于 2023-03-23 20:22:41 发布