50
50
51
51
## 解法
52
52
53
- 定义指针 ` p ` 、 ` q ` 分别指向头节点和下一个节点, ` pre ` 指向头节点的前一个节点。
53
+ ** 方法一:头插法 **
54
54
55
- 遍历链表,改变指针 ` p ` 指向的节点的指向,将其指向 ` pre ` 指针指向的节点,即 ` p.next = pre ` 。然后 ` pre ` 指针指向 ` p ` , ` p ` 、 ` q ` 指针往前走 。
55
+ 创建虚拟头节点 $dummy$,遍历链表,将每个节点依次插入 $dummy$ 的下一个节点。遍历结束,返回 $dummy.next$ 。
56
56
57
- 当遍历结束后,返回 ` pre ` 指针即可。
57
+ ** 方法二:递归**
58
+
59
+ 递归反转链表的第二个节点到尾部的所有节点,然后 head 插在反转后的链表的尾部。
58
60
59
61
<!-- tabs:start -->
60
62
63
65
``` python
64
66
# Definition for singly-linked list.
65
67
# 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
70
71
class Solution :
71
72
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
+ ```
79
82
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
81
97
```
82
98
83
99
### ** Java**
@@ -90,19 +106,22 @@ class Solution:
90
106
* public class ListNode {
91
107
* int val;
92
108
* 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; }
94
112
* }
95
113
*/
96
114
class Solution {
97
115
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;
104
123
}
105
- return pre ;
124
+ return dummy . next ;
106
125
}
107
126
}
108
127
```
@@ -115,18 +134,20 @@ class Solution {
115
134
* public class ListNode {
116
135
* int val;
117
136
* 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; }
119
140
* }
120
141
*/
121
142
class Solution {
122
143
public ListNode reverseList (ListNode head ) {
123
144
if (head == null || head. next == null ) {
124
145
return head;
125
146
}
126
- ListNode res = reverseList(head. next);
147
+ ListNode ans = reverseList(head. next);
127
148
head. next. next = head;
128
149
head. next = null ;
129
- return res ;
150
+ return ans ;
130
151
}
131
152
}
132
153
```
@@ -146,14 +167,15 @@ class Solution {
146
167
* @return {ListNode}
147
168
*/
148
169
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;
155
177
}
156
- return pre ;
178
+ return dummy . next ;
157
179
};
158
180
```
159
181
@@ -168,14 +190,34 @@ var reverseList = function (head) {
168
190
* }
169
191
*/
170
192
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
177
200
}
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
179
221
}
180
222
```
181
223
@@ -195,16 +237,39 @@ func reverseList(head *ListNode) *ListNode {
195
237
class Solution {
196
238
public:
197
239
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 )
201
243
{
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 ;
206
248
}
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;
208
273
}
209
274
};
210
275
```
0 commit comments