题目链接:https://leetcode.com/problems/reverse-linked-list-ii/
解题思路:
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 class Solution { 10 public ListNode reverseBetween(ListNode head, int m, int n) { 11 12 13 ListNode newhead = new ListNode(-1); 14 newhead.next = head; 15 16 if(head==null || head.next ==null) 17 return head; 18 19 ListNode startpoint = newhead; 20 ListNode node1 = null; 21 ListNode node2 = null; 22 23 for(int i=0;i<n;i++) 24 { 25 if(i<m-1) 26 { 27 startpoint = startpoint.next; 28 } 29 else if(i==m-1) 30 { 31 node1 = startpoint.next; 32 node2 = node1.next; 33 } 34 else 35 { 36 node1.next = node2.next; 37 node2.next = startpoint.next; 38 startpoint.next = node2; 39 node2 = node1.next; 40 } 41 } 42 return newhead.next; 43 44 } 45 }