Skip to content

Commit a8a330a

Browse files
committed
feat: add solutions to lc problem: No.0206
No.0206.Reverse Linked List
1 parent aeab1d7 commit a8a330a

File tree

7 files changed

+267
-137
lines changed

7 files changed

+267
-137
lines changed

solution/0200-0299/0206.Reverse Linked List/README.md

+113-48
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@
5050

5151
## 解法
5252

53-
定义指针 `p``q` 分别指向头节点和下一个节点,`pre` 指向头节点的前一个节点。
53+
**方法一:头插法**
5454

55-
遍历链表,改变指针 `p` 指向的节点的指向,将其指向 `pre` 指针指向的节点,即 `p.next = pre`。然后 `pre` 指针指向 `p``p``q` 指针往前走
55+
创建虚拟头节点 $dummy$,遍历链表,将每个节点依次插入 $dummy$ 的下一个节点。遍历结束,返回 $dummy.next$
5656

57-
当遍历结束后,返回 `pre` 指针即可。
57+
**方法二:递归**
58+
59+
递归反转链表的第二个节点到尾部的所有节点,然后 head 插在反转后的链表的尾部。
5860

5961
<!-- tabs:start -->
6062

@@ -63,21 +65,35 @@
6365
```python
6466
# Definition for singly-linked list.
6567
# class ListNode:
66-
# def __init__(self, x):
67-
# self.val = x
68-
# self.next = None
69-
68+
# def __init__(self, val=0, next=None):
69+
# self.val = val
70+
# self.next = next
7071
class Solution:
7172
def reverseList(self, head: ListNode) -> ListNode:
72-
previous, current, next = None, head, None
73-
74-
while current is not None:
75-
next = current.next
76-
current.next = previous
77-
previous = current
78-
current = next
73+
dummy = ListNode()
74+
curr = head
75+
while curr:
76+
next = curr.next
77+
curr.next = dummy.next
78+
dummy.next = curr
79+
curr = next
80+
return dummy.next
81+
```
7982

80-
return previous
83+
```python
84+
# Definition for singly-linked list.
85+
# class ListNode:
86+
# def __init__(self, val=0, next=None):
87+
# self.val = val
88+
# self.next = next
89+
class Solution:
90+
def reverseList(self, head: ListNode) -> ListNode:
91+
if head is None or head.next is None:
92+
return head
93+
ans = self.reverseList(head.next)
94+
head.next.next = head
95+
head.next = None
96+
return ans
8197
```
8298

8399
### **Java**
@@ -90,19 +106,22 @@ class Solution:
90106
* public class ListNode {
91107
* int val;
92108
* ListNode next;
93-
* ListNode(int x) { val = x; }
109+
* ListNode() {}
110+
* ListNode(int val) { this.val = val; }
111+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
94112
* }
95113
*/
96114
class Solution {
97115
public ListNode reverseList(ListNode head) {
98-
ListNode pre = null, p = head;
99-
while (p != null) {
100-
ListNode q = p.next;
101-
p.next = pre;
102-
pre = p;
103-
p = q;
116+
ListNode dummy = new ListNode();
117+
ListNode curr = head;
118+
while (curr != null) {
119+
ListNode next = curr.next;
120+
curr.next = dummy.next;
121+
dummy.next = curr;
122+
curr = next;
104123
}
105-
return pre;
124+
return dummy.next;
106125
}
107126
}
108127
```
@@ -115,18 +134,20 @@ class Solution {
115134
* public class ListNode {
116135
* int val;
117136
* ListNode next;
118-
* ListNode(int x) { val = x; }
137+
* ListNode() {}
138+
* ListNode(int val) { this.val = val; }
139+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
119140
* }
120141
*/
121142
class Solution {
122143
public ListNode reverseList(ListNode head) {
123144
if (head == null || head.next == null) {
124145
return head;
125146
}
126-
ListNode res = reverseList(head.next);
147+
ListNode ans = reverseList(head.next);
127148
head.next.next = head;
128149
head.next = null;
129-
return res;
150+
return ans;
130151
}
131152
}
132153
```
@@ -146,14 +167,15 @@ class Solution {
146167
* @return {ListNode}
147168
*/
148169
var reverseList = function (head) {
149-
let pre = null;
150-
for (let p = head; p; ) {
151-
let q = p.next;
152-
p.next = pre;
153-
pre = p;
154-
p = q;
170+
let dummy = new ListNode();
171+
let curr = head;
172+
while (curr) {
173+
let next = curr.next;
174+
curr.next = dummy.next;
175+
dummy.next = curr;
176+
curr = next;
155177
}
156-
return pre;
178+
return dummy.next;
157179
};
158180
```
159181

@@ -168,14 +190,34 @@ var reverseList = function (head) {
168190
* }
169191
*/
170192
func reverseList(head *ListNode) *ListNode {
171-
var pre *ListNode
172-
for p := head; p != nil; {
173-
q := p.Next
174-
p.Next = pre
175-
pre = p
176-
p = q
193+
dummy := &ListNode{}
194+
curr := head
195+
for curr != nil {
196+
next := curr.Next
197+
curr.Next = dummy.Next
198+
dummy.Next = curr
199+
curr = next
177200
}
178-
return pre
201+
return dummy.Next
202+
}
203+
```
204+
205+
```go
206+
/**
207+
* Definition for singly-linked list.
208+
* type ListNode struct {
209+
* Val int
210+
* Next *ListNode
211+
* }
212+
*/
213+
func reverseList(head *ListNode) *ListNode {
214+
if head == nil || head.Next == nil {
215+
return head
216+
}
217+
ans := reverseList(head.Next)
218+
head.Next.Next = head
219+
head.Next = nil
220+
return ans
179221
}
180222
```
181223

@@ -195,16 +237,39 @@ func reverseList(head *ListNode) *ListNode {
195237
class Solution {
196238
public:
197239
ListNode* reverseList(ListNode* head) {
198-
ListNode* pre = nullptr;
199-
ListNode* p = head;
200-
while (p)
240+
ListNode* dummy = new ListNode();
241+
ListNode* curr = head;
242+
while (curr)
201243
{
202-
ListNode* q = p->next;
203-
p->next = pre;
204-
pre = p;
205-
p = q;
244+
ListNode* next = curr->next;
245+
curr->next = dummy->next;
246+
dummy->next = curr;
247+
curr = next;
206248
}
207-
return pre;
249+
return dummy->next;
250+
}
251+
};
252+
```
253+
254+
```cpp
255+
/**
256+
* Definition for singly-linked list.
257+
* struct ListNode {
258+
* int val;
259+
* ListNode *next;
260+
* ListNode() : val(0), next(nullptr) {}
261+
* ListNode(int x) : val(x), next(nullptr) {}
262+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
263+
* };
264+
*/
265+
class Solution {
266+
public:
267+
ListNode* reverseList(ListNode* head) {
268+
if (!head || !head->next) return head;
269+
ListNode* ans = reverseList(head->next);
270+
head->next->next = head;
271+
head->next = nullptr;
272+
return ans;
208273
}
209274
};
210275
```

0 commit comments

Comments
 (0)