---
title: "LeetCode #876: Middle of the Linked List"
url: https://perrotta.dev/2025/12/leetcode-%23876-middle-of-the-linked-list/
last_updated: 2026-01-03
---


[LeetCode #876: Middle of the Linked List](https://leetcode.com/problems/leetcode-#876:-middle-of-the-linked-list):

```python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head:
            return head

        if not head.next:
            return head

        if not head.next.next:
            return head.next

        slow = head
        fast = head

        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

        return slow
```

This works too:

```python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = head
        fast = head

        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

        return slow
```

