题目如下:
https://leetcode.cn/problems/middle-of-the-linked-list/description/
Java代码如下:
`
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 static void main(String[] args) {
}
public static ListNode middleNode(ListNode head) {
ListNode h1=head,h2=h1;
while (h2.next!=null&&h2.next.next!=null){
h1=h1.next;
h2=h2.next.next;
}
if (h2.next!=null){
return h1.next;
}
return h1;
}
}
`