Java [Likou 876] Intermediate node of linked list

Given a  head non-empty singly linked list whose head node is , return the middle node of the linked list.

If there are two intermediate nodes, return the second intermediate node.

 code show as below:

/**
 * 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 fast=head;
        ListNode low=head;
        while (fast!=null&&fast.next!=null){
            low=low.next;
            fast=fast.next.next;
        }
        return low;
    }
}

Achieving the result:

Guess you like

Origin blog.csdn.net/m0_62218217/article/details/121736485