题解 | #链表内指定区间反转#
链表内指定区间反转
https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode temp = head;
ListNode pre = new ListNode(0);
ListNode p = null;
ListNode q = null;
ListNode g = null;
for(int i=1; i<m; i++){
pre.next = temp;
temp = temp.next;
}
g = temp;
for(int i=0; i<=n-m; i++){
q = temp.next;
temp.next = p;
p = temp;
temp = q;
}
if(pre.next == null){
head = p;
}
else{
pre.next.next = p;
}
g.next = temp;
return head;
}
}
#一次遍历解决单链表中部分链表的反转#