题解 | #链表内指定区间反转#

链表内指定区间反转

http://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
  public ListNode reverseBetween (ListNode head, int m, int n) {
        if(m==n) return head;
        ArrayList<Integer> list=new ArrayList<>();
        ArrayList<Integer> list1=new ArrayList<>();
        ListNode current=head;
        while (current!=null){
            list.add(current.val);
            current=current.next;
        }
        for (int i = m-1; i < n; i++) {
            list1.add(list.get(i));
        }
        Collections.reverse(list1);
        int index=0;
        for (int i = m-1; i < n; i++) {
            list.set(i,list1.get(index++));
        }
        current=head;
        int i=0;
        while (current!=null){
            current.val=list.get(i);
            i++;
            current=current.next;
        }
        return head;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-16 14:00
机械打工仔:来挂自己了,经典巨婴从校园投入职场
点赞 评论 收藏
分享
07-01 23:23
郑州大学 Java
否极泰来来来来:牛客迟早有高三的
点赞 评论 收藏
分享
07-16 19:23
门头沟学院 Java
仁者伍敌:专业技能好多,好强
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务