leetcode 876. 链表的中间结点
题目描述:
本题作为找链表中间结点的标准方法。
这道题虽然简单,但是是进阶题目的基础,例如第2095题2095. Delete the Middle Node of a Linked List。
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* middleNode(ListNode* head) {ListNode* slow = head;ListNode* fast = head;while(fast && fast->next){fast = fast->next->next;slow = slow->next;}return slow;}
};