题解 | #链表内指定区间反转#
链表内指定区间反转
https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类 */ public ListNode reverseBetween (ListNode head, int m, int n) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode g = dummy; ListNode p = head; for(int i = 0;i<m-1;i++){ g = g.next; p = p.next; } for(int i = 0;i<n-m;i++){ ListNode remove = p.next; p.next = p.next.next; remove.next = g.next; g.next = remove; } return dummy.next; } }