赞
踩
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode reverseBetween(ListNode head, int left, int right) { ListNode firstHead = new ListNode(0,head); ListNode secondHead = new ListNode(0,head); ListNode result = firstHead; int len = 0; while(len != left - 1){ firstHead = firstHead.next; secondHead = secondHead.next; len++; } secondHead = secondHead.next; for(int i = 0;i < right - left;i++){ ListNode temp = secondHead.next; secondHead.next = temp.next; temp.next = firstHead.next; firstHead.next = temp; } return result.next; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。