image-20220320214159624.png

    直接快慢指针

    一个两步走到底

    慢的走到中间

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode() {}
    7. * ListNode(int val) { this.val = val; }
    8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    9. * }
    10. */
    11. class Solution {
    12. public ListNode middleNode(ListNode head) {
    13. ListNode slow = head, fast = head;
    14. while (fast != null && fast.next != null) {
    15. fast = fast.next.next;
    16. slow = slow.next;
    17. }
    18. return slow;
    19. }
    20. }