> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/876.middle-of-the-linked-list.md).

# 876.Middle of the Linked List

## 1.問題

![](/files/-LOQK6FOKVgqvPpOk1g1)

## 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;
    }
};
```
