# Leetcode 876. Middle of the Linked List ###### tags: `Leetcode(C++)` 題目 : https://leetcode.com/problems/middle-of-the-linked-list/ 。 想法 : 先遍歷一次陣列得知總共有幾個數,再出來走一次。 時間複雜度 : O(n)。 程式碼 : ``` /** * 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) { int n[110],tol=0; ListNode *tmp=head; while(head != nullptr){ n[tol++]=head->val; head=head->next; } for(int i=0 ; i<tol/2 ; i++){ tmp=tmp->next; } return tmp; } }; ```
×
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