From 3fdb4a491f7b1deef9f2bd1ee3357a07ee00d3c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Tue, 26 Jul 2022 15:58:12 +0800 Subject: [PATCH] =?UTF-8?q?BM2=20=E9=93=BE=E8=A1=A8=E5=86=85=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E5=8C=BA=E9=97=B4=E5=8F=8D=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../huangge1199/nowcoder/bm/BM2/Solution.java | 40 +++++++++++++++++++ .../huangge1199/nowcoder/common/ListNode.java | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 bm/src/main/java/com/huangge1199/nowcoder/bm/BM2/Solution.java diff --git a/bm/src/main/java/com/huangge1199/nowcoder/bm/BM2/Solution.java b/bm/src/main/java/com/huangge1199/nowcoder/bm/BM2/Solution.java new file mode 100644 index 0000000..edcd889 --- /dev/null +++ b/bm/src/main/java/com/huangge1199/nowcoder/bm/BM2/Solution.java @@ -0,0 +1,40 @@ +package com.huangge1199.nowcoder.bm.BM2; + +import com.huangge1199.nowcoder.common.ListNode; + +/** + * @author hyy + * @Classname Solution + * @Description BM2 链表内指定区间反转 + * @Date 2022/7/26 15:57 + */ +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 + ListNode cur = head; + ListNode bef = new ListNode(0); + bef.next = head; + ListNode pre = bef; + int index = 1; + while (index < n) { + if (index < m) { + pre = cur; + cur = cur.next; + } else { + ListNode tmp = cur.next; + cur.next = tmp.next; + tmp.next = pre.next; + pre.next = tmp; + } + index++; + } + return bef.next; + } +} diff --git a/common/src/main/java/com/huangge1199/nowcoder/common/ListNode.java b/common/src/main/java/com/huangge1199/nowcoder/common/ListNode.java index 103d094..b17e481 100644 --- a/common/src/main/java/com/huangge1199/nowcoder/common/ListNode.java +++ b/common/src/main/java/com/huangge1199/nowcoder/common/ListNode.java @@ -13,7 +13,7 @@ public class ListNode { public int val; public ListNode next = null; - ListNode(int val) { + public ListNode(int val) { this.val = val; } }