ListNode* middleNode(ListNode* head) {
        ListNode* slow = head;
        ListNode* fast = head;
        while (fast != NULL && fast->next != NULL) {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }

自己写的不够简洁

ListNode *middleNode(ListNode *head) {
    if (head == nullptr || head->next == nullptr)
        return head;
    ListNode *slow = head, *fast = head->next;
    while (fast->next != nullptr) {
        fast = fast->next;
        slow = slow->next;
        if (fast->next == nullptr)
            return slow;
        fast = fast->next;
    }
    return slow->next;
}

相关文章:

  • 2022-02-23
  • 2021-11-24
  • 2021-06-04
  • 2021-07-28
  • 2021-12-31
  • 2022-12-23
猜你喜欢
  • 2022-02-25
  • 2021-07-07
  • 2021-07-08
  • 2022-02-20
  • 2022-02-21
  • 2022-12-23
相关资源
相似解决方案