File tree Expand file tree Collapse file tree 4 files changed +179
-0
lines changed
src/main/java/leetcode/_92_ Expand file tree Collapse file tree 4 files changed +179
-0
lines changed Original file line number Diff line number Diff line change
1
+ ### [ 92\. Reverse Linked List II] ( https://leetcode.com/problems/reverse-linked-list-ii/ )
2
+
3
+ Difficulty: ** Medium**
4
+
5
+
6
+ Reverse a linked list from position _ m_ to _ n_ . Do it in one-pass.
7
+
8
+ ** Note: ** 1 ≤ _ m_ ≤ _ n_ ≤ length of list.
9
+
10
+ ** Example:**
11
+
12
+ ```
13
+ Input: 1->2->3->4->5->NULL, m = 2, n = 4
14
+ Output: 1->4->3->2->5->NULL
15
+ ```
16
+
17
+
18
+ #### Solution
19
+
20
+ Language: ** Java**
21
+
22
+ ``` java
23
+ /**
24
+ * Definition for singly-linked list.
25
+ * public class ListNode {
26
+ * int val;
27
+ * ListNode next;
28
+ * ListNode(int x) { val = x; }
29
+ * }
30
+ */
31
+ class Solution {
32
+ public ListNode reverseBetween (ListNode head , int m , int n ) {
33
+ ListNode fakeNode = new ListNode (- 1 );
34
+ fakeNode. next = head;
35
+ ListNode p = fakeNode;
36
+ ListNode q = head;
37
+ int index = 1 ;
38
+ while (q. next != null ) {
39
+ if (index >= m && index < n) {
40
+ ListNode node = q. next;
41
+ q. next = q. next. next;
42
+ node. next = p. next;
43
+ p. next = node;
44
+ } else {
45
+ q = q. next;
46
+ }
47
+ if (index < m) {
48
+ p = p. next;
49
+ }
50
+ if (++ index >= n) {
51
+ break ;
52
+ }
53
+ }
54
+ return fakeNode. next;
55
+ }
56
+ }
57
+ ```
58
+ ![ pic] ( https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-08-14-nLuLW6.jpg )
Original file line number Diff line number Diff line change
1
+ package leetcode ._92_ ;
2
+
3
+ import leetcode .common .ListNode ;
4
+
5
+ /**
6
+ * Created by zhangbo54 on 2019-03-04.
7
+ */
8
+ public class Main {
9
+ public static void main (String [] args ) {
10
+ Solution solution = new Solution ();
11
+
12
+ ListNode node1 = new ListNode (1 );
13
+ ListNode node2 = new ListNode (2 );
14
+ ListNode node32 = new ListNode (3 );
15
+ ListNode node41 = new ListNode (4 );
16
+ ListNode node5 = new ListNode (5 );
17
+ node1 .next = node2 ;
18
+ node2 .next = node32 ;
19
+ node32 .next = node41 ;
20
+ node41 .next = node5 ;
21
+
22
+ System .out .println (solution .reverseBetween (node1 , 2 , 4 ));
23
+ }
24
+ }
25
+
Original file line number Diff line number Diff line change
1
+ package leetcode ._92_ ;
2
+
3
+ import leetcode .common .ListNode ;
4
+
5
+ /**
6
+ * Definition for singly-linked list.
7
+ * public class ListNode {
8
+ * int val;
9
+ * ListNode next;
10
+ * ListNode(int x) { val = x; }
11
+ * }
12
+ */
13
+ class Solution {
14
+ public ListNode reverseBetween (ListNode head , int m , int n ) {
15
+ ListNode fakeNode = new ListNode (-1 );
16
+ fakeNode .next = head ;
17
+ ListNode p = fakeNode ;
18
+ ListNode q = head ;
19
+ int index = 1 ;
20
+ while (q .next != null ) {
21
+ if (index >= m && index < n ) {
22
+ ListNode node = q .next ;
23
+ q .next = q .next .next ;
24
+ node .next = p .next ;
25
+ p .next = node ;
26
+ } else {
27
+ q = q .next ;
28
+ }
29
+ if (index < m ) {
30
+ p = p .next ;
31
+ }
32
+ if (++index >= n ) {
33
+ break ;
34
+ }
35
+ }
36
+ return fakeNode .next ;
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ ### [ 92\. Reverse Linked List II] ( https://leetcode.com/problems/reverse-linked-list-ii/ )
2
+
3
+ Difficulty: ** Medium**
4
+
5
+
6
+ Reverse a linked list from position _ m_ to _ n_ . Do it in one-pass.
7
+
8
+ ** Note: ** 1 ≤ _ m_ ≤ _ n_ ≤ length of list.
9
+
10
+ ** Example:**
11
+
12
+ ```
13
+ Input: 1->2->3->4->5->NULL, m = 2, n = 4
14
+ Output: 1->4->3->2->5->NULL
15
+ ```
16
+
17
+
18
+ #### Solution
19
+
20
+ Language: ** Java**
21
+
22
+ ``` java
23
+ /**
24
+ * Definition for singly-linked list.
25
+ * public class ListNode {
26
+ * int val;
27
+ * ListNode next;
28
+ * ListNode(int x) { val = x; }
29
+ * }
30
+ */
31
+ class Solution {
32
+ public ListNode reverseBetween (ListNode head , int m , int n ) {
33
+ ListNode fakeNode = new ListNode (- 1 );
34
+ fakeNode. next = head;
35
+ ListNode p = fakeNode;
36
+ ListNode q = head;
37
+ int index = 1 ;
38
+ while (q. next != null ) {
39
+ if (index >= m && index < n) {
40
+ ListNode node = q. next;
41
+ q. next = q. next. next;
42
+ node. next = p. next;
43
+ p. next = node;
44
+ } else {
45
+ q = q. next;
46
+ }
47
+ if (index < m) {
48
+ p = p. next;
49
+ }
50
+ if (++ index >= n) {
51
+ break ;
52
+ }
53
+ }
54
+ return fakeNode. next;
55
+ }
56
+ }
57
+ ```
58
+ ![ pic] ( https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-08-14-nLuLW6.jpg )
You can’t perform that action at this time.
0 commit comments