diff --git a/animation-simulation/链表篇/面试题 02.03. 链表中间节点.md b/animation-simulation/链表篇/面试题 02.03. 链表中间节点.md index a1bffb0..b18baab 100644 --- a/animation-simulation/链表篇/面试题 02.03. 链表中间节点.md +++ b/animation-simulation/链表篇/面试题 02.03. 链表中间节点.md @@ -71,6 +71,7 @@ class Solution { C++ Code: ```cpp +class Solution { public: ListNode* middleNode(ListNode* head) { ListNode * fast = head;//快指针 @@ -86,3 +87,34 @@ public: }; ``` +JS Code: + +```js +var middleNode = function(head) { + let fast = head;//快指针 + let slow = head;//慢指针 + //循环条件,思考一下跳出循环的情况 + while (fast && fast.next) { + fast = fast.next.next; + slow = slow.next; + } + //返回slow指针指向的节点 + return slow +}; +``` + +Python Code: + +```py +class Solution: + def middleNode(self, head: ListNode) -> ListNode: + fast = head # 快指针 + slow = head # 慢指针 + # 循环条件,思考一下跳出循环的情况 + while fast is not None and fast.next is not None: + fast = fast.next.next + slow = slow.next + # 返回slow指针指向的节点 + return slow +``` +