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

链表内指定区间反转

https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Fintelligent%3FquestionJobId%3D10%26tagId%3D21000

import java.util.*;
class ListNode {
    int val;
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(ListNode next) {
        this.next = next;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    @Override
    public String toString() {
        return "ListNode{" +
                "val=" + val +
                ", next=" + next +
                '}';
    }
}

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {

    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * @param head ListNode类
     * @param m    int整型
     * @param n    int整型
     * @return ListNode类
     */

    public static void main(String[] args) {

//        ListNode r = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
        ListNode r = new ListNode(5, null);
        Solution s = new Solution();
        System.out.println("++++++++ result +++++++");
        System.out.println(s.reverseBetween(r, 1, 1));

        // 创建链表
    }

    public static ListNode findEndNode(ListNode h) {
        if (h == null) {
            return null;
        }
        while (h.next != null) {
            h = h.next;
        }
        return h;
    }

    public ListNode reverseBetween(ListNode head, int m, int n) {
        // write code here
        ListNode result = head;
        ListNode m_node = Solution.findIndexNode(head, m - 1);
        ListNode n_node = Solution.findIndexNode(head, n + 1);

        ListNode subListNode = Solution.subListNode(head, m, n);
        ListNode reverseLiseNode = Solution.reverseListNode(subListNode);// 原地转

        if (m_node == null) {
            result = reverseLiseNode;
        } else {
            m_node.next = reverseLiseNode;
        }

        // 新链表
        while (head.next != null) {
            head = head.next;
        }
        head.next = n_node;


//        return
        return result;
    }

    public static ListNode findIndexNode(ListNode head, int n) {
        if (head == null || n == 0) {
            return null;
        }
        ListNode result = null;
        int index = 0;
        while (true) {
            index++;
            if (index < n) {
                result = head;
                head = head.next;
                continue;
            }
            if (index == n) {
                result = head;
                break;
            }
            if (head == null) {
                result = null;
                break;
            }
        }
        return result;
    }

    public static ListNode subListNode(ListNode head, int m, int n) {
        if (head == null) {
            return null;
        }
        // 至少一个节点
        int index = 0;
        ListNode sub = null;
        while (true) {
            // 记录到了第index个节点
            index++;
            // 未到头节点
            if (index < m) {
                head = head.next;
                continue;
            }
            // 找到头节点
            if (index == m) {
                // 记录头节点
                sub = head;
                continue;
            }

            // 在区间里面
            if (index > m && index <= n) {
                // 移动指针
                head = head.next;
                continue;
            }
            // 找完了
            if (head == null) {
                break;
            }
            if (index > n) {
                head.next = null;
                break;
            }
        }
        return sub;
    }

    public static ListNode reverseListNode(ListNode head) {
        if (head.next == null) {
            return head;
        }
        ListNode rest = reverseListNode(head.next);
        head.next.next = head;
        head.next = null;
        return rest;

    }

}

全部评论

相关推荐

10-11 17:45
门头沟学院 Java
走吗:别怕 我以前也是这么认为 虽然一面就挂 但是颇有收获!
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务