您现在的位置是:主页 > news > 域名做网站/云浮seo

域名做网站/云浮seo

admin2025/5/23 23:02:36news

简介域名做网站,云浮seo,浙江网站开发,app网站排名题目链接:https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c 分析: 原链表:m 2 ,n 4 反转后 1、需要获取到m开始节点 2、翻转m - n的节点 3、m 节点的 next 指针指向 n 节点的 next 4、m−1 节点的 ne…

域名做网站,云浮seo,浙江网站开发,app网站排名题目链接:https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c 分析: 原链表:m 2 ,n 4 反转后 1、需要获取到m开始节点 2、翻转m - n的节点 3、m 节点的 next 指针指向 n 节点的 next 4、m−1 节点的 ne…

题目链接:https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c

 分析:

原链表:m = 2 ,n = 4 

反转后 

1、需要获取到m开始节点

2、翻转m - n的节点

3、m 节点的 next 指针指向 n 节点的 next

4、m−1 节点的 next 指针指向 n 节点

5、如果m=1,这时,m-1 节点是不存在的,在链表的头节点之前再加一个空节点来指向头节点

代码:

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) {if(m==n){return head;}// write code hereListNode d = new ListNode(-1);d.next = head;ListNode pre = d;//要翻转的前驱结点for(int i=1;i<m;i++){pre = pre.next;}head = pre.next;//m头ListNode next; //用来中转//m-n翻转for(int i=m; i<n;i++){next = head.next;head.next = next.next;next.next = pre.next;pre.next = next;}return d.next;}
}