Skip to content

Commit e932212

Browse files
committedJun 10, 2021
feat: update solutions to lc problem: No.0092. Reverse Linked List II
1 parent 723f456 commit e932212

File tree

8 files changed

+408
-135
lines changed

8 files changed

+408
-135
lines changed
 

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

+147-26
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,23 @@
5252
```python
5353
# Definition for singly-linked list.
5454
# class ListNode:
55-
# def __init__(self, x):
56-
# self.val = x
57-
# self.next = None
58-
55+
# def __init__(self, val=0, next=None):
56+
# self.val = val
57+
# self.next = next
5958
class Solution:
60-
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
61-
if head is None or head.next is None or m == n:
59+
def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:
60+
if head is None or head.next is None or left == right:
6261
return head
63-
dummy = ListNode(0)
64-
dummy.next = head
65-
pre, cur = dummy, head
66-
for _ in range(m - 1):
67-
pre = cur
68-
cur = cur.next
69-
p, q = pre, cur
70-
for _ in range(n - m + 1):
62+
dummy = ListNode(0, head)
63+
pre = dummy
64+
for _ in range(left - 1):
65+
pre = pre.next
66+
p, q = pre, pre.next
67+
cur = q
68+
for _ in range(right - left + 1):
7169
t = cur.next
7270
cur.next = pre
73-
pre = cur
74-
cur = t
71+
pre, cur = cur, t
7572
p.next = pre
7673
q.next = cur
7774
return dummy.next
@@ -87,23 +84,147 @@ class Solution:
8784
* public class ListNode {
8885
* int val;
8986
* ListNode next;
90-
* ListNode(int x) { val = x; }
87+
* ListNode() {}
88+
* ListNode(int val) { this.val = val; }
89+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
90+
* }
91+
*/
92+
class Solution {
93+
public ListNode reverseBetween(ListNode head, int left, int right) {
94+
if (head == null || head.next == null || left == right) {
95+
return head;
96+
}
97+
ListNode dummy = new ListNode(0, head);
98+
ListNode pre = dummy;
99+
for (int i = 0; i < left - 1; ++i) {
100+
pre = pre.next;
101+
}
102+
ListNode p = pre;
103+
ListNode q = pre.next;
104+
ListNode cur = q;
105+
for (int i = 0; i < right - left + 1; ++i) {
106+
ListNode t = cur.next;
107+
cur.next = pre;
108+
pre = cur;
109+
cur = t;
110+
}
111+
p.next = pre;
112+
q.next = cur;
113+
return dummy.next;
114+
}
115+
}
116+
```
117+
118+
### **JavaScript**
119+
120+
```js
121+
/**
122+
* Definition for singly-linked list.
123+
* function ListNode(val, next) {
124+
* this.val = (val===undefined ? 0 : val)
125+
* this.next = (next===undefined ? null : next)
91126
* }
92127
*/
128+
/**
129+
* @param {ListNode} head
130+
* @param {number} left
131+
* @param {number} right
132+
* @return {ListNode}
133+
*/
134+
var reverseBetween = function(head, left, right) {
135+
if (!head || !head.next || left == right) {
136+
return head;
137+
}
138+
const dummy = new ListNode(0, head);
139+
let pre = dummy;
140+
for (let i = 0; i < left - 1; ++i) {
141+
pre = pre.next;
142+
}
143+
const p = pre;
144+
const q = pre.next;
145+
let cur = q;
146+
for (let i = 0; i < right - left + 1; ++i) {
147+
const t = cur.next;
148+
cur.next = pre;
149+
pre = cur;
150+
cur = t;
151+
}
152+
p.next = pre;
153+
q.next = cur;
154+
return dummy.next;
155+
};
156+
```
157+
158+
### **C++**
159+
160+
```cpp
161+
/**
162+
* Definition for singly-linked list.
163+
* struct ListNode {
164+
* int val;
165+
* ListNode *next;
166+
* ListNode() : val(0), next(nullptr) {}
167+
* ListNode(int x) : val(x), next(nullptr) {}
168+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
169+
* };
170+
*/
93171
class Solution {
94-
public ListNode reverseBetween(ListNode head, int m, int n) {
95-
if (head == null || head.next == null || m == n) {
172+
public:
173+
ListNode* reverseBetween(ListNode* head, int left, int right) {
174+
if (head == nullptr || head->next == nullptr || left == right) {
96175
return head;
97176
}
98-
ListNode dummy = new ListNode(0);
99-
dummy.next = head;
100-
ListNode pre = dummy, cur = head;
101-
for (int i = 0; i < m - 1; ++i) {
177+
ListNode *dummy = new ListNode(0, head);
178+
ListNode *pre = dummy;
179+
for (int i = 0; i < left - 1; ++i) {
180+
pre = pre->next;
181+
}
182+
ListNode *p = pre, *q = pre->next;
183+
ListNode *cur = q;
184+
for (int i = 0; i < right - left + 1; ++i) {
185+
ListNode *t = cur->next;
186+
cur->next = pre;
102187
pre = cur;
103-
cur = cur.next;
188+
cur = t;
189+
}
190+
p->next = pre;
191+
q->next = cur;
192+
return dummy->next;
193+
}
194+
};
195+
```
196+
197+
### **C#**
198+
199+
```cs
200+
/**
201+
* Definition for singly-linked list.
202+
* public class ListNode {
203+
* public int val;
204+
* public ListNode next;
205+
* public ListNode(int val=0, ListNode next=null) {
206+
* this.val = val;
207+
* this.next = next;
208+
* }
209+
* }
210+
*/
211+
public class Solution {
212+
public ListNode ReverseBetween(ListNode head, int left, int right) {
213+
if (head == null || head.next == null || left == right)
214+
{
215+
return head;
216+
}
217+
ListNode dummy = new ListNode(0, head);
218+
ListNode pre = dummy;
219+
for (int i = 0; i < left - 1; ++i)
220+
{
221+
pre = pre.next;
104222
}
105-
ListNode p = pre, q = cur;
106-
for (int i = 0; i < n - m + 1; ++i) {
223+
ListNode p = pre;
224+
ListNode q = pre.next;
225+
ListNode cur = q;
226+
for (int i = 0; i < right - left + 1; ++i)
227+
{
107228
ListNode t = cur.next;
108229
cur.next = pre;
109230
pre = cur;

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

+147-26
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,23 @@
4343
```python
4444
# Definition for singly-linked list.
4545
# class ListNode:
46-
# def __init__(self, x):
47-
# self.val = x
48-
# self.next = None
49-
46+
# def __init__(self, val=0, next=None):
47+
# self.val = val
48+
# self.next = next
5049
class Solution:
51-
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
52-
if head is None or head.next is None or m == n:
50+
def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:
51+
if head is None or head.next is None or left == right:
5352
return head
54-
dummy = ListNode(0)
55-
dummy.next = head
56-
pre, cur = dummy, head
57-
for _ in range(m - 1):
58-
pre = cur
59-
cur = cur.next
60-
p, q = pre, cur
61-
for _ in range(n - m + 1):
53+
dummy = ListNode(0, head)
54+
pre = dummy
55+
for _ in range(left - 1):
56+
pre = pre.next
57+
p, q = pre, pre.next
58+
cur = q
59+
for _ in range(right - left + 1):
6260
t = cur.next
6361
cur.next = pre
64-
pre = cur
65-
cur = t
62+
pre, cur = cur, t
6663
p.next = pre
6764
q.next = cur
6865
return dummy.next
@@ -76,23 +73,147 @@ class Solution:
7673
* public class ListNode {
7774
* int val;
7875
* ListNode next;
79-
* ListNode(int x) { val = x; }
76+
* ListNode() {}
77+
* ListNode(int val) { this.val = val; }
78+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
79+
* }
80+
*/
81+
class Solution {
82+
public ListNode reverseBetween(ListNode head, int left, int right) {
83+
if (head == null || head.next == null || left == right) {
84+
return head;
85+
}
86+
ListNode dummy = new ListNode(0, head);
87+
ListNode pre = dummy;
88+
for (int i = 0; i < left - 1; ++i) {
89+
pre = pre.next;
90+
}
91+
ListNode p = pre;
92+
ListNode q = pre.next;
93+
ListNode cur = q;
94+
for (int i = 0; i < right - left + 1; ++i) {
95+
ListNode t = cur.next;
96+
cur.next = pre;
97+
pre = cur;
98+
cur = t;
99+
}
100+
p.next = pre;
101+
q.next = cur;
102+
return dummy.next;
103+
}
104+
}
105+
```
106+
107+
### **JavaScript**
108+
109+
```js
110+
/**
111+
* Definition for singly-linked list.
112+
* function ListNode(val, next) {
113+
* this.val = (val===undefined ? 0 : val)
114+
* this.next = (next===undefined ? null : next)
80115
* }
81116
*/
117+
/**
118+
* @param {ListNode} head
119+
* @param {number} left
120+
* @param {number} right
121+
* @return {ListNode}
122+
*/
123+
var reverseBetween = function(head, left, right) {
124+
if (!head || !head.next || left == right) {
125+
return head;
126+
}
127+
const dummy = new ListNode(0, head);
128+
let pre = dummy;
129+
for (let i = 0; i < left - 1; ++i) {
130+
pre = pre.next;
131+
}
132+
const p = pre;
133+
const q = pre.next;
134+
let cur = q;
135+
for (let i = 0; i < right - left + 1; ++i) {
136+
const t = cur.next;
137+
cur.next = pre;
138+
pre = cur;
139+
cur = t;
140+
}
141+
p.next = pre;
142+
q.next = cur;
143+
return dummy.next;
144+
};
145+
```
146+
147+
### **C++**
148+
149+
```cpp
150+
/**
151+
* Definition for singly-linked list.
152+
* struct ListNode {
153+
* int val;
154+
* ListNode *next;
155+
* ListNode() : val(0), next(nullptr) {}
156+
* ListNode(int x) : val(x), next(nullptr) {}
157+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
158+
* };
159+
*/
82160
class Solution {
83-
public ListNode reverseBetween(ListNode head, int m, int n) {
84-
if (head == null || head.next == null || m == n) {
161+
public:
162+
ListNode* reverseBetween(ListNode* head, int left, int right) {
163+
if (head == nullptr || head->next == nullptr || left == right) {
85164
return head;
86165
}
87-
ListNode dummy = new ListNode(0);
88-
dummy.next = head;
89-
ListNode pre = dummy, cur = head;
90-
for (int i = 0; i < m - 1; ++i) {
166+
ListNode *dummy = new ListNode(0, head);
167+
ListNode *pre = dummy;
168+
for (int i = 0; i < left - 1; ++i) {
169+
pre = pre->next;
170+
}
171+
ListNode *p = pre, *q = pre->next;
172+
ListNode *cur = q;
173+
for (int i = 0; i < right - left + 1; ++i) {
174+
ListNode *t = cur->next;
175+
cur->next = pre;
91176
pre = cur;
92-
cur = cur.next;
177+
cur = t;
178+
}
179+
p->next = pre;
180+
q->next = cur;
181+
return dummy->next;
182+
}
183+
};
184+
```
185+
186+
### **C#**
187+
188+
```cs
189+
/**
190+
* Definition for singly-linked list.
191+
* public class ListNode {
192+
* public int val;
193+
* public ListNode next;
194+
* public ListNode(int val=0, ListNode next=null) {
195+
* this.val = val;
196+
* this.next = next;
197+
* }
198+
* }
199+
*/
200+
public class Solution {
201+
public ListNode ReverseBetween(ListNode head, int left, int right) {
202+
if (head == null || head.next == null || left == right)
203+
{
204+
return head;
205+
}
206+
ListNode dummy = new ListNode(0, head);
207+
ListNode pre = dummy;
208+
for (int i = 0; i < left - 1; ++i)
209+
{
210+
pre = pre.next;
93211
}
94-
ListNode p = pre, q = cur;
95-
for (int i = 0; i < n - m + 1; ++i) {
212+
ListNode p = pre;
213+
ListNode q = pre.next;
214+
ListNode cur = q;
215+
for (int i = 0; i < right - left + 1; ++i)
216+
{
96217
ListNode t = cur.next;
97218
cur.next = pre;
98219
pre = cur;

0 commit comments

Comments
 (0)
Please sign in to comment.