每天刷一道牛客题霸-第14天-链表内指定区间反转
题目
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) { // write code here ListNode virtualRoot = new ListNode(0); virtualRoot.next = head; ListNode preStart = virtualRoot; ListNode start = head; for (int i = 1; i < m; i++) { preStart = start; start = start.next; } for (int i = 0; i < n - m; i++) { ListNode temp = start.next; start.next = temp.next; temp.next = preStart.next; preStart.next = temp; } return virtualRoot.next; } }#牛客题霸##题解#