mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-03 06:21:33 +00:00
添加py和js
This commit is contained in:
@@ -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
|
||||||
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user