--- title: 'LeetCode 876. Middle of the Linked List' disqus: hackmd --- # LeetCode 876. Middle of the Linked List ## Description Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node. ## Example ![](https://i.imgur.com/PKt6u5z.png) Input: head = [1,2,3,4,5] Output: [3,4,5] Explanation: The middle node of the list is node 3. ## Constraints The number of nodes in the list is in the range [1, 100]. 1 <= Node.val <= 100 ## Answer 此題可用slow表一次走一步,fast表一次走兩步,直到fast或fast的下一點為NULL為止,即可得中間點。 ```Cin= struct ListNode* middleNode(struct ListNode* head){ struct ListNode* slow = head , *fast = head; while(fast!=NULL && fast->next!=NULL){ slow = slow->next; fast = fast->next->next; } return slow; } ``` ## Link https://leetcode.com/problems/middle-of-the-linked-list/ ###### tags: `Leetcode`