Skip to content

Commit d0112c9

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0092
1 parent 77e6126 commit d0112c9

File tree

6 files changed

+165
-21
lines changed

6 files changed

+165
-21
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
1. [合并 K 个排序链表](/solution/0000-0099/0023.Merge%20k%20Sorted%20Lists/README.md)
6363
1. [排序链表](/solution/0100-0199/0148.Sort%20List/README.md)
6464
1. [反转链表](/solution/0200-0299/0206.Reverse%20Linked%20List/README.md)
65+
1. [反转链表 II](/solution/0000-0099/0092.Reverse%20Linked%20List%20II/README.md)
6566
1. [重排链表](/solution/0100-0199/0143.Reorder%20List/README.md)
6667
1. [旋转链表](/solution/0000-0099/0061.Rotate%20List/README.md)
6768
1. [回文链表](/solution/0200-0299/0234.Palindrome%20Linked%20List/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
6060
1. [Merge k Sorted Lists](/solution/0000-0099/0023.Merge%20k%20Sorted%20Lists/README_EN.md)
6161
1. [Sort List](/solution/0100-0199/0148.Sort%20List/README_EN.md)
6262
1. [Reverse Linked List](/solution/0200-0299/0206.Reverse%20Linked%20List/README_EN.md)
63+
1. [Reverse Linked List II](/solution/0000-0099/0092.Reverse%20Linked%20List%20II/README_EN.md)
6364
1. [Reorder List](/solution/0100-0199/0143.Reorder%20List/README_EN.md)
6465
1. [Rotate List](/solution/0000-0099/0061.Rotate%20List/README_EN.md)
6566
1. [Palindrome Linked List](/solution/0200-0299/0234.Palindrome%20Linked%20List/README_EN.md)

solution/0000-0099/0092.Reverse Linked List II/README.md

+57-2
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,70 @@
2626
<!-- 这里可写当前语言的特殊实现逻辑 -->
2727

2828
```python
29-
29+
# Definition for singly-linked list.
30+
# class ListNode:
31+
# def __init__(self, x):
32+
# self.val = x
33+
# self.next = None
34+
35+
class Solution:
36+
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
37+
if head is None or head.next is None or m == n:
38+
return head
39+
dummy = ListNode(0)
40+
dummy.next = head
41+
pre, cur = dummy, head
42+
for _ in range(m - 1):
43+
pre = cur
44+
cur = cur.next
45+
p, q = pre, cur
46+
for _ in range(n - m + 1):
47+
t = cur.next
48+
cur.next = pre
49+
pre = cur
50+
cur = t
51+
p.next = pre
52+
q.next = cur
53+
return dummy.next
3054
```
3155

3256
### **Java**
3357

3458
<!-- 这里可写当前语言的特殊实现逻辑 -->
3559

3660
```java
37-
61+
/**
62+
* Definition for singly-linked list.
63+
* public class ListNode {
64+
* int val;
65+
* ListNode next;
66+
* ListNode(int x) { val = x; }
67+
* }
68+
*/
69+
class Solution {
70+
public ListNode reverseBetween(ListNode head, int m, int n) {
71+
if (head == null || head.next == null || m == n) {
72+
return head;
73+
}
74+
ListNode dummy = new ListNode(0);
75+
dummy.next = head;
76+
ListNode pre = dummy, cur = head;
77+
for (int i = 0; i < m - 1; ++i) {
78+
pre = cur;
79+
cur = cur.next;
80+
}
81+
ListNode p = pre, q = cur;
82+
for (int i = 0; i < n - m + 1; ++i) {
83+
ListNode t = cur.next;
84+
cur.next = pre;
85+
pre = cur;
86+
cur = t;
87+
}
88+
p.next = pre;
89+
q.next = cur;
90+
return dummy.next;
91+
}
92+
}
3893
```
3994

4095
### **...**

solution/0000-0099/0092.Reverse Linked List II/README_EN.md

+57-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,68 @@
2525
### **Python3**
2626

2727
```python
28-
28+
# Definition for singly-linked list.
29+
# class ListNode:
30+
# def __init__(self, x):
31+
# self.val = x
32+
# self.next = None
33+
34+
class Solution:
35+
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
36+
if head is None or head.next is None or m == n:
37+
return head
38+
dummy = ListNode(0)
39+
dummy.next = head
40+
pre, cur = dummy, head
41+
for _ in range(m - 1):
42+
pre = cur
43+
cur = cur.next
44+
p, q = pre, cur
45+
for _ in range(n - m + 1):
46+
t = cur.next
47+
cur.next = pre
48+
pre = cur
49+
cur = t
50+
p.next = pre
51+
q.next = cur
52+
return dummy.next
2953
```
3054

3155
### **Java**
3256

3357
```java
34-
58+
/**
59+
* Definition for singly-linked list.
60+
* public class ListNode {
61+
* int val;
62+
* ListNode next;
63+
* ListNode(int x) { val = x; }
64+
* }
65+
*/
66+
class Solution {
67+
public ListNode reverseBetween(ListNode head, int m, int n) {
68+
if (head == null || head.next == null || m == n) {
69+
return head;
70+
}
71+
ListNode dummy = new ListNode(0);
72+
dummy.next = head;
73+
ListNode pre = dummy, cur = head;
74+
for (int i = 0; i < m - 1; ++i) {
75+
pre = cur;
76+
cur = cur.next;
77+
}
78+
ListNode p = pre, q = cur;
79+
for (int i = 0; i < n - m + 1; ++i) {
80+
ListNode t = cur.next;
81+
cur.next = pre;
82+
pre = cur;
83+
cur = t;
84+
}
85+
p.next = pre;
86+
q.next = cur;
87+
return dummy.next;
88+
}
89+
}
3590
```
3691

3792
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
19
class Solution {
210
public ListNode reverseBetween(ListNode head, int m, int n) {
3-
ListNode dummy = new ListNode(-1);
11+
if (head == null || head.next == null || m == n) {
12+
return head;
13+
}
14+
ListNode dummy = new ListNode(0);
415
dummy.next = head;
5-
ListNode pre = dummy;
6-
int i = 0;
7-
for (; i < m - 1; ++i) {
8-
pre = pre.next;
16+
ListNode pre = dummy, cur = head;
17+
for (int i = 0; i < m - 1; ++i) {
18+
pre = cur;
19+
cur = cur.next;
920
}
10-
11-
ListNode head2 = pre;
12-
pre = pre.next;
13-
ListNode cur = pre.next;
14-
for (; i < n - 1; ++i) {
15-
pre.next = cur.next;
16-
cur.next = head2.next;
17-
head2.next = cur;
18-
cur = pre.next;
21+
ListNode p = pre, q = cur;
22+
for (int i = 0; i < n - m + 1; ++i) {
23+
ListNode t = cur.next;
24+
cur.next = pre;
25+
pre = cur;
26+
cur = t;
1927
}
20-
28+
p.next = pre;
29+
q.next = cur;
2130
return dummy.next;
22-
23-
2431
}
2532
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
9+
if head is None or head.next is None or m == n:
10+
return head
11+
dummy = ListNode(0)
12+
dummy.next = head
13+
pre, cur = dummy, head
14+
for _ in range(m - 1):
15+
pre = cur
16+
cur = cur.next
17+
p, q = pre, cur
18+
for _ in range(n - m + 1):
19+
t = cur.next
20+
cur.next = pre
21+
pre = cur
22+
cur = t
23+
p.next = pre
24+
q.next = cur
25+
return dummy.next

0 commit comments

Comments
 (0)