File tree Expand file tree Collapse file tree 2 files changed +94
-0
lines changed
java/com/chen/algorithm/study/test92 Expand file tree Collapse file tree 2 files changed +94
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .chen .algorithm .study .test92 ;
2
+
3
+
4
+ /**
5
+ * @author Chen WeiJie
6
+ * @date 2020-05-27 17:39:32
7
+ **/
8
+ public class Solution {
9
+
10
+
11
+ public class ListNode {
12
+ int val ;
13
+ ListNode next ;
14
+
15
+ ListNode (int x ) {
16
+ val = x ;
17
+ }
18
+ }
19
+
20
+
21
+ public ListNode reverseBetween (ListNode head , int m , int n ) {
22
+
23
+ if (head == null ) {
24
+ return null ;
25
+ }
26
+
27
+ ListNode pre = new ListNode (1 );
28
+ pre .next = head ;
29
+ ListNode ans = pre ;
30
+
31
+ for (int i = 0 ; i < m - 1 ; i ++) {
32
+ pre = pre .next ;
33
+ }
34
+
35
+ ListNode end = pre .next ;
36
+
37
+ for (int i = m ; i < n ; i ++) {
38
+
39
+ ListNode temp = end .next ;
40
+ end .next = temp .next ;
41
+ temp .next = pre .next ;
42
+ pre .next = temp ;
43
+ }
44
+
45
+ return ans .next ;
46
+ }
47
+ }
Original file line number Diff line number Diff line change
1
+ package com .chen .test ;
2
+
3
+ /**
4
+ * @author Chen WeiJie
5
+ * @date 2020-05-27 17:26:50
6
+ **/
7
+ public class TestLinkRevert {
8
+
9
+
10
+ /**
11
+ * Definition for singly-linked list.
12
+ * public class ListNode {
13
+ * int val;
14
+ * ListNode next;
15
+ * ListNode(int x) { val = x; }
16
+ * }
17
+ */
18
+ public class ListNode {
19
+ int val ;
20
+ ListNode next ;
21
+
22
+ ListNode (int x ) {
23
+ val = x ;
24
+ }
25
+ }
26
+
27
+ class Solution {
28
+
29
+ public ListNode reverseBetween (ListNode head , int m , int n ) {
30
+ ListNode pre = new ListNode (0 );
31
+ ListNode ans = pre ;
32
+ pre .next = head ;
33
+ for (int i = 0 ; i < m - 1 ; i ++) {
34
+ pre = pre .next ;
35
+ }
36
+ ListNode end = pre .next ;
37
+ for (int i = m ; i < n ; i ++) {
38
+ ListNode tmp = end .next ;
39
+ end .next = tmp .next ;
40
+ tmp .next = pre .next ;
41
+ pre .next = tmp ;
42
+ }
43
+ return ans .next ;
44
+ }
45
+ }
46
+
47
+ }
You can’t perform that action at this time.
0 commit comments