leetcode 206 补充js版解法

This commit is contained in:
daluozha 2021-04-27 18:12:18 +08:00
parent e8255d73ec
commit 39683f1794

View File

@ -33,6 +33,7 @@ low = temp 即可。然后重复执行上诉操作直至最后,这样则完成
我会对每个关键点进行注释大家可以参考动图理解
Java Code:
```java
class Solution {
public ListNode reverseList(ListNode head) {
@ -62,9 +63,27 @@ class Solution {
}
```
JS Code:
```javascript
var reverseList = function(head) {
if(!head || !head.next) {
return head;
}
let low = null;
let pro = head;
while (pro) {
let temp = pro;
pro = pro.next;
temp.next = low;
low = temp;
}
return low;
};
```
上面的迭代写法是不是搞懂啦现在还有一种递归写法不是特别容易理解刚开始刷题的同学可以只看迭代解法
Java Code:
```java
class Solution {
public ListNode reverseList(ListNode head) {
@ -86,3 +105,16 @@ class Solution {
```
JS Code:
```javascript
var reverseList = function(head) {
if (!head || !head.next) {
return head;
}
let pro = reverseList(head.next);
head.next.next = head;
head.next = null;
return pro;
};
```