Skip to content

Commit 39683f1

Browse files
committed
leetcode 206 补充js版解法
1 parent e8255d7 commit 39683f1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

animation-simulation/链表篇/leetcode206反转链表.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ low = temp 即可。然后重复执行上诉操作直至最后,这样则完成
3333

3434
我会对每个关键点进行注释,大家可以参考动图理解。
3535

36+
Java Code:
3637
```java
3738
class Solution {
3839
public ListNode reverseList(ListNode head) {
@@ -62,9 +63,27 @@ class Solution {
6263

6364
}
6465
```
66+
JS Code:
67+
```javascript
68+
var reverseList = function(head) {
69+
if(!head || !head.next) {
70+
return head;
71+
}
72+
let low = null;
73+
let pro = head;
74+
while (pro) {
75+
let temp = pro;
76+
pro = pro.next;
77+
temp.next = low;
78+
low = temp;
79+
}
80+
return low;
81+
};
82+
```
6583

6684
上面的迭代写法是不是搞懂啦,现在还有一种递归写法,不是特别容易理解,刚开始刷题的同学,可以只看迭代解法。
6785

86+
Java Code:
6887
```java
6988
class Solution {
7089
public ListNode reverseList(ListNode head) {
@@ -86,3 +105,16 @@ class Solution {
86105

87106
```
88107

108+
JS Code:
109+
```javascript
110+
var reverseList = function(head) {
111+
if (!head || !head.next) {
112+
return head;
113+
}
114+
let pro = reverseList(head.next);
115+
head.next.next = head;
116+
head.next = null;
117+
return pro;
118+
};
119+
```
120+

0 commit comments

Comments
 (0)