leetcode — reverse-linked-list-ii

/**
 * Source : https://oj.leetcode.com/problems/reverse-linked-list-ii/
 *
 *
 * Reverse a linked list from position m to n. Do it in-place and in one-pass.
 *
 * For example:
 * Given 1->2->3->4->5->NULL, m = 2 and n = 4,
 *
 * return 1->4->3->2->5->NULL.
 *
 * Note:
 * Given m, n satisfy the following condition:
 * 1 ≤ m ≤ n ≤ length of list.
 */
public class ReverseLinkedList2 {


    /**
     *
     * 反转单向链表指定范围内的元素
     *
     * 需要考虑第一个元素是否被翻转
     *
      * @param head
     * @param m
     * @param n
     * @return
     */
    public Node reverse (Node head, int m, int n) {
        if (head == null) {
            return head;
        }
        Node dummy = new Node();
        dummy.next = head;
        int pos = 1;
        Node unreverseListLast = dummy;
        Node reverseListLast = null;
        Node cur = head;
        Node next = null;
        Node pre = dummy;
        while (cur != null && pos <= n) {
            next = cur.next;
            if (pos == m) {
                unreverseListLast = pre;
                reverseListLast = cur;
            } else if (pos > m) {
                // 在制定范围内,反转
                cur.next = pre;
            }
            pre = cur;
            cur = next;
            pos++;
        }

        // 反转完指定范围内的元素,将反转部分和未反转部分连接起来
        unreverseListLast.next = pre;
        reverseListLast.next = cur;

        return dummy.next;
    }





    private static class Node {
        int value;
        Node next;

        @Override
        public String toString() {
            return "Node{" +
                    "value=" + value +
                    ", next=" + (next == null ? "" : next.value) +
                    '}';
        }
    }

    private static void print (Node node) {
        while (node != null) {
            System.out.println(node);
            node = node.next;
        }
        System.out.println();
    }

    public Node createList (int[] arr) {
        if (arr.length == 0) {
            return null;
        }
        Node head = new Node();
        head.value = arr[0];
        Node pointer = head;
        for (int i = 1; i < arr.length; i++) {
            Node node = new Node();
            node.value = arr[i];
            pointer.next = node;
            pointer = pointer.next;
        }
        return head;
    }

    public static void main(String[] args) {
        ReverseLinkedList2 reverseLinkedList2 = new ReverseLinkedList2();
        print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{1,2,3,4,5}), 2, 4));
        print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{1,2,3,4,5}), 2, 2));
        print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{1,2,3,4,5}), 2, 5));
        print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{1,2,3,4,5}), 1, 5));
        print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{}), 1, 1));
    }

}
posted @ 2017-11-02 22:25  lacker  阅读(153)  评论(0编辑  收藏  举报