mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-05 15:12:22 +00:00
链表专题更新cpp代码
This commit is contained in:
@@ -48,6 +48,10 @@
|
||||
|
||||

|
||||
|
||||
**题目代码**
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user