链表专题更新cpp代码

This commit is contained in:
3119005212
2021-04-28 18:28:00 +08:00
parent 6d96954aa7
commit afd452aeda
15 changed files with 627 additions and 12 deletions

View File

@@ -48,6 +48,10 @@
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210321131249789.gif)
**题目代码**
Java Code:
```java
class Solution {
public ListNode middleNode(ListNode head) {
@@ -64,3 +68,21 @@ class Solution {
}
```
C++ Code:
```cpp
public:
ListNode* middleNode(ListNode* head) {
ListNode * fast = head;//快指针
ListNode * slow = head;//慢指针
//循环条件,思考一下跳出循环的情况
while (fast != nullptr && fast->next != nullptr) {
fast = fast->next->next;
slow = slow->next;
}
//返回slow指针指向的节点
return slow;
}
};
```