Skip to content

Commit dff62af

Browse files
committed
添加py和js
1 parent 3acd44f commit dff62af

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

animation-simulation/链表篇/面试题 02.03. 链表中间节点.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Solution {
7171
C++ Code:
7272

7373
```cpp
74+
class Solution {
7475
public:
7576
ListNode* middleNode(ListNode* head) {
7677
ListNode * fast = head;//快指针
@@ -86,3 +87,34 @@ public:
8687
};
8788
```
8889
90+
JS Code:
91+
92+
```js
93+
var middleNode = function(head) {
94+
let fast = head;//快指针
95+
let slow = head;//慢指针
96+
//循环条件,思考一下跳出循环的情况
97+
while (fast && fast.next) {
98+
fast = fast.next.next;
99+
slow = slow.next;
100+
}
101+
//返回slow指针指向的节点
102+
return slow
103+
};
104+
```
105+
106+
Python Code:
107+
108+
```py
109+
class Solution:
110+
def middleNode(self, head: ListNode) -> ListNode:
111+
fast = head # 快指针
112+
slow = head # 慢指针
113+
# 循环条件,思考一下跳出循环的情况
114+
while fast is not None and fast.next is not None:
115+
fast = fast.next.next
116+
slow = slow.next
117+
# 返回slow指针指向的节点
118+
return slow
119+
```
120+

0 commit comments

Comments
 (0)