From 39683f1794d576ca3e5531fcbf06546fe05e5ce9 Mon Sep 17 00:00:00 2001 From: daluozha <561890199@qq.com> Date: Tue, 27 Apr 2021 18:12:18 +0800 Subject: [PATCH] =?UTF-8?q?leetcode=20206=20=E8=A1=A5=E5=85=85js=E7=89=88?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../链表篇/leetcode206反转链表.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/animation-simulation/链表篇/leetcode206反转链表.md b/animation-simulation/链表篇/leetcode206反转链表.md index 3130b04..93b6261 100644 --- a/animation-simulation/链表篇/leetcode206反转链表.md +++ b/animation-simulation/链表篇/leetcode206反转链表.md @@ -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; +}; +``` +