添加py和js

This commit is contained in:
jaredliw 2021-07-12 17:24:38 +08:00
parent 3acd44f653
commit dff62af40a

View File

@ -71,6 +71,7 @@ class Solution {
C++ Code: C++ Code:
```cpp ```cpp
class Solution {
public: public:
ListNode* middleNode(ListNode* head) { ListNode* middleNode(ListNode* head) {
ListNode * fast = 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
```