mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-11-28 06:48:53 +00:00
leetcode 206 补充js版解法
This commit is contained in:
parent
e8255d73ec
commit
39683f1794
@ -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;
|
||||
};
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user