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

链表内指定区间反转

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:10
已编辑
门头沟学院 人工智能
写这篇之前我犹豫了挺久。一方面是怕被人骂,&quot;又一个收割焦虑的转行帖&quot;;另一方面是看了太多用&nbsp;GPT&nbsp;套娃出来的「学习路线」文章,AI&nbsp;味重得让人没法读完。所以这篇全是亲身踩过的坑,时间线、用过的项目、当时的心路全都尽量原样写出来。如果你是大学生在迷茫要不要转&nbsp;AI,或者已经在转的路上,希望能给点参考。&nbsp;一个反共识的开场:你以为进&nbsp;OpenAI&nbsp;的人都是博士?&nbsp;先讲个故事,跟我没关系,但跟所有想转&nbsp;AI&nbsp;的人都有关系。&nbsp;OpenAI&nbsp;的&nbsp;Sora&nbsp;团队(就是搞文生视频那个)一共&nbsp;13&nbsp;个人。这里面有两个人特别有意思:&nbsp;Will&nbsp;DePue,密歇根大学计算机系,直接辍学了。17...
_hengheng:我也本,也算是做ai相关,我最开始感觉做ai工程师有多么多么困难,后来发现懂了原理后整体训练完全可以看成一个流程化的内容,开源方案太多了,大多基本都是按着模子在自家业务上做各种操作,就算是大厂的小部门也没那么多资源去训基模,反而更多的是像怎么把技术往业务方向靠近了,不过当前时代如果本科学历没那么好加上自己执行力不是特别强还真不建议走ai工程师这条路,可以试试其他ai的偏业务方向,不然校招不太好杀出来
点赞 评论 收藏
分享
再懒也要睡懒觉:大学4年玩的挺爽的哈😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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