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

链表内指定区间反转

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;

    }

}

全部评论

相关推荐

07-02 18:09
门头沟学院 Java
苍穹外卖和谷粒商城这俩是不是烂大街了,还能做吗?
想去重庆的鸽子在吐槽:你不如把这俩做完自己搞明白再优化点再来问 何必贩卖焦虑
点赞 评论 收藏
分享
06-23 11:43
门头沟学院 Java
allin校招的烤冷...:我靠,今天中午我也是这个hr隔一个星期发消息给我。问的问题还是一模一样的😅
点赞 评论 收藏
分享
06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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