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

# 01/13/2022 876

using floyd's loop dectection algorithm. The fast pointer travers twice as slow pointer. when fast reachers the end of the list, slow is in the middle.

Time O(n) Space O(1)

class Solution { public ListNode middleNode(ListNode head) {

```
    ListNode slow = head;
    ListNode fast = head;
    
    while(fast!= null && fast.next != null){
        fast = fast.next.next;
        slow = slow.next;
    }
    
    return slow;
    
}
```

}

class Solution { public ListNode middleNode(ListNode head) {

```
    ListNode slow = head;
    ListNode fast = head;
    
    while(fast.next!= null && fast.next.next != null){
        fast = fast.next.next;
        slow = slow.next;
    }
    
    if(fast.next != null){//even number of node
        slow = slow.next;
    }
    return slow;
    
}
```

}
