链表的中间结点 (Middle of the Linked List)

 

思路:

// @Title: 链表的中间结点 (Middle of the Linked List)
// @Author: qisiii
// @Date: 2022-03-10 20:06:43
// @Runtime: 0 ms
// @Memory: 39.1 MB
// @comment: 
// @flag: 
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode slow=head;
        ListNode fast=head;
        while(fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        return slow;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18