|
2 | 2 |
|
3 | 3 | import com.fishercoder.common.classes.ListNode;
|
4 | 4 |
|
5 |
| -/** |
6 |
| - * 92. Reverse Linked List II |
7 |
| - * |
8 |
| - * Reverse a linked list from position m to n. Do it in-place and in one-pass. |
9 |
| - * |
10 |
| - * For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, |
11 |
| - * |
12 |
| - * return 1->4->3->2->5->NULL. |
13 |
| - * |
14 |
| - * Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list. |
15 |
| - */ |
16 | 5 | public class _92 {
|
17 | 6 |
|
18 |
| - public static class Solution1 { |
19 |
| - /** credit: https://discuss.leetcode.com/topic/8976/simple-java-solution-with-clear-explanation */ |
20 |
| - public ListNode reverseBetween(ListNode head, int m, int n) { |
21 |
| - // use four nodes, pre, start, then, dummy |
22 |
| - // just reverse the nodes along the way |
23 |
| - ListNode dummy = new ListNode(-1); |
24 |
| - dummy.next = head; |
25 |
| - ListNode pre = dummy; |
26 |
| - for (int i = 0; i < m - 1; i++) { |
27 |
| - pre = pre.next; |
28 |
| - } |
| 7 | + public static class Solution1 { |
| 8 | + /** |
| 9 | + * credit: https://discuss.leetcode.com/topic/8976/simple-java-solution-with-clear-explanation |
| 10 | + */ |
| 11 | + public ListNode reverseBetween(ListNode head, int m, int n) { |
| 12 | + // use four nodes, pre, start, then, dummy |
| 13 | + // just reverse the nodes along the way |
| 14 | + ListNode dummy = new ListNode(-1); |
| 15 | + dummy.next = head; |
| 16 | + ListNode pre = dummy; |
| 17 | + for (int i = 0; i < m - 1; i++) { |
| 18 | + pre = pre.next; |
| 19 | + } |
29 | 20 |
|
30 |
| - ListNode start = pre.next;// start is the node prior to reversing, in the given example, |
31 |
| - // start is node with value 1 |
32 |
| - ListNode then = start.next;// then is the node that we'll start to reverse, in the given |
33 |
| - // example, it's 2 |
| 21 | + ListNode start = pre.next;// start is the node prior to reversing, in the given example, |
| 22 | + // start is node with value 1 |
| 23 | + ListNode then = start.next;// then is the node that we'll start to reverse, in the given |
| 24 | + // example, it's 2 |
34 | 25 |
|
35 |
| - for (int i = 0; i < n - m; i++) { |
36 |
| - // pay special attention to this for loop, it's assigning then.next to start.next, it |
37 |
| - // didn't initialize a new node |
38 |
| - // this does exactly what I desired to do, but I just didn't figure out how to implement |
39 |
| - // it, thumbs up to the OP! |
40 |
| - start.next = then.next; |
41 |
| - then.next = pre.next; |
42 |
| - pre.next = then; |
43 |
| - then = start.next; |
44 |
| - } |
| 26 | + for (int i = 0; i < n - m; i++) { |
| 27 | + // pay special attention to this for loop, it's assigning then.next to start.next, it |
| 28 | + // didn't initialize a new node |
| 29 | + // this does exactly what I desired to do, but I just didn't figure out how to implement |
| 30 | + // it, thumbs up to the OP! |
| 31 | + start.next = then.next; |
| 32 | + then.next = pre.next; |
| 33 | + pre.next = then; |
| 34 | + then = start.next; |
| 35 | + } |
45 | 36 |
|
46 |
| - return dummy.next; |
| 37 | + return dummy.next; |
| 38 | + } |
47 | 39 | }
|
48 |
| - } |
49 | 40 | }
|
0 commit comments