# 876.Middle of the Linked List

## 1.問題

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LOQJeFSiwHxq4FW8_9z%2F-LOQK6FOKVgqvPpOk1g1%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-10-10%20%E4%B8%8A%E5%8D%888.15.23.png?alt=media\&token=5246fc67-1985-4ad5-8bb5-f88b91cad116)

## 2.想法

* 快慢指針

## 3.程式碼

```
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        if (!head || !head->next) {
            return head;
        }
        
        ListNode *first = head, *second = head;
        
        while (second && second->next) {
            first = first->next;
            second = second->next->next;
        }
        
        return first;
    }
};
```
