# 876. Middle of the Linked List Difficulty: Easy ## Solution ```cpp= /** *** Author: R-CO *** E-mail: daniel1820kobe@gmail.com *** Date: 2020-10-13 **/ #include <cstdlib> #include <vector> using std::vector; 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) { if (head == nullptr) { return nullptr; } vector<ListNode *> nodes_vector; while (head != nullptr) { nodes_vector.emplace_back(head); head = head->next; } return nodes_vector[nodes_vector.size() >> 1]; } }; int main(int argc, char *argv[]) { return EXIT_SUCCESS; } ``` ## Result Success Details Runtime: 0 ms, faster than 100.00% of C++ online submissions for Middle of the Linked List. Memory Usage: 7 MB, less than 99.41% of C++ online submissions for Middle of the Linked List. ###### tags: `LeetCode-Easy` `C++`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up