Skip to content

Commit bac0f71

Browse files
committed
feat: update leetcode solution No.206
1 parent d51eb28 commit bac0f71

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

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

+27-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class Solution:
4747

4848
### **Java**
4949

50+
迭代版本:
51+
5052
```java
5153
/**
5254
* Definition for singly-linked list.
@@ -58,8 +60,7 @@ class Solution:
5860
*/
5961
class Solution {
6062
public ListNode reverseList(ListNode head) {
61-
ListNode pre = null;
62-
ListNode p = head;
63+
ListNode pre = null, p = head;
6364
while (p != null) {
6465
ListNode q = p.next;
6566
p.next = pre;
@@ -71,6 +72,30 @@ class Solution {
7172
}
7273
```
7374

75+
递归版本:
76+
77+
```java
78+
/**
79+
* Definition for singly-linked list.
80+
* public class ListNode {
81+
* int val;
82+
* ListNode next;
83+
* ListNode(int x) { val = x; }
84+
* }
85+
*/
86+
class Solution {
87+
public ListNode reverseList(ListNode head) {
88+
if (head == null || head.next == null) {
89+
return head;
90+
}
91+
ListNode res = reverseList(head.next);
92+
head.next.next = head;
93+
head.next = null;
94+
return res;
95+
}
96+
}
97+
```
98+
7499
### **JavaScript**
75100

76101
```js

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

+27-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Solution:
4646

4747
### **Java**
4848

49+
Iterative version:
50+
4951
```java
5052
/**
5153
* Definition for singly-linked list.
@@ -57,8 +59,7 @@ class Solution:
5759
*/
5860
class Solution {
5961
public ListNode reverseList(ListNode head) {
60-
ListNode pre = null;
61-
ListNode p = head;
62+
ListNode pre = null, p = head;
6263
while (p != null) {
6364
ListNode q = p.next;
6465
p.next = pre;
@@ -70,6 +71,30 @@ class Solution {
7071
}
7172
```
7273

74+
Recursive version:
75+
76+
```java
77+
/**
78+
* Definition for singly-linked list.
79+
* public class ListNode {
80+
* int val;
81+
* ListNode next;
82+
* ListNode(int x) { val = x; }
83+
* }
84+
*/
85+
class Solution {
86+
public ListNode reverseList(ListNode head) {
87+
if (head == null || head.next == null) {
88+
return head;
89+
}
90+
ListNode res = reverseList(head.next);
91+
head.next.next = head;
92+
head.next = null;
93+
return res;
94+
}
95+
}
96+
```
97+
7398
### **JavaScript**
7499

75100
```js

solution/0200-0299/0206.Reverse Linked List/Solution.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
*/
99
class Solution {
1010
public ListNode reverseList(ListNode head) {
11-
ListNode pre = null;
12-
ListNode p = head;
11+
ListNode pre = null, p = head;
1312
while (p != null) {
1413
ListNode q = p.next;
1514
p.next = pre;

0 commit comments

Comments
 (0)