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

链表内指定区间反转

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

import java.util.*;

/*

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

public class Solution { /** * * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类 */ public ListNode reverseBetween (ListNode head, int m, int n) { // write code here

    if(head == null){
        return null;
    }
    if(head.next==null || m==n){
        return head;
    }
    
    ListNode startNode1 = null;
    ListNode startNode2 = null;
    ListNode endNode1 = null;
    ListNode endNode2 = null;
    ListNode copyHead = head;
    ListNode copyHead2 = head;
    
    if(m==1){
        startNode1=startNode2 = head;
        for(int i=1;i<n;i++){
            head = head.next;
        }
        endNode1 = head;
        endNode2 = head.next;
        endNode1.next = null;
        ListNode reverseList=null;
        reverseList = ReverseList(startNode1);
        copyHead = reverseList;
        while(reverseList.next!=null){
            reverseList = reverseList.next;
        }
        reverseList.next = endNode2;
        return copyHead;
    }else{
        for(int i=1;i<m-1;i++){
            head = head.next;
        }
        startNode1 = head;
        startNode2 = head.next;
        startNode1.next=null;
        head = startNode2;
        
        
        for(int i=m;i<n;i++){
            head = head.next;
        }
        endNode1 = head;
        endNode2 = head.next;
        endNode1.next = null;
        ListNode reverseList=null;
        
        reverseList=ReverseList(startNode2);
        ListNode end = reverseList;
        while(end.next!=null){
            end = end.next;
        }
        startNode1.next=reverseList;
        end.next = endNode2;
        return copyHead;
    } 
}

public ListNode ReverseList(ListNode head) {
    if(head==null){
        return null;
    }
    ListNode currentNode = null;
    ListNode temp = null;
    ListNode endNode = null;
    if(head.next==null){
        return head;
    }else{
        currentNode = head.next;
        head.next = null;
        endNode = head;
    }
    while(currentNode!=null){
        temp = currentNode.next;
        currentNode.next = head;
        head = currentNode;
        currentNode = temp;
    }
    return head;
    
}

}

全部评论

相关推荐

03-26 08:58
已编辑
门头沟学院 Java
ttl:&nbsp;3.19一面晚上过3.20二面3.23oc3.25offerbase:末9有一段中小厂实习一面面经:(总体时长一个小时二十分钟左右没什么八股,主要都是问项目和场景题1.实习(问了有四十分钟,感觉面试官很看重实习这一块,一直在拷打,问到后面我都要疯了,好在准备得比较充分1️⃣用的是什么中间件,有参与技术选型吗,实习的项目里为什么选这个RabbitMQ而不是kafka,为什么不用RocketMQ,为什么放弃异步,自己的项目里面使用的是kafka,那你觉得项目和实习的中间件选型有差异的原因是什么,他们之间的区别在哪里,底层的原因知道吗(高柱到这里已经快疯了,但是硬着头皮答完了,主要是从一致性吞吐量和框架的契合度答,面试官说答得挺好的,应该是没什么问题,这一块就问了快半个小时,到这里我已经快疯了2️⃣项目怎么对接上下游3️⃣介绍项目的难点重点4️⃣微服务(高柱实习是单体项目没涉及这一块5️⃣Redis的使用2.项目:1️⃣智能客服是怎么应用在项目里的(langchain4j➕rag➕functioncalling)2️⃣RAG了解多少3️⃣文本向量化的难点是什么,了解哪些大模型的知识(我一点不懂,纯瞎扯,但貌似扯对了4️⃣对ai的态度是什么,aicoding相关5️⃣怎么保证多节点下Caffeine缓存里面数据都是一致的(答的是短ttl,面试官不是很满意,但是我确实不太懂这个怎么保证,后来查了还是不懂怎么保证6️⃣Redis的使用,和你的实习项目的使用有区别吗,还有一些引申问题3.八股(含量不高,就是走个过场1️⃣进程的内存布局2️⃣Redis三剑客3️⃣微服务相关知识(高柱已经忘得差不多了…勉强答上来4️⃣JVM5️⃣线程状态6️⃣线程安全,在你的实习项目里怎么保证线程安全的(又绕回来了4.智商题找异常球5.手撕:1️⃣五道sql,不难2️⃣力扣不重叠的滑动窗口数组,贪心➕双指针秒了强度拉满了这个一面,高柱到后面人都是傻的二面面经:(就半个小时实习拷打,简历上写了几点就问了几点,问完就结束了,无手撕
查看19道真题和解析
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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