题目描述:
给定一个带有头结点 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
class Solution {
public ListNode
middleNode(ListNode head
) {
if(head
==null
|| head
.next
==null
){
return head
;
}
ListNode fast
= head
;
ListNode slow
= head
;
while(fast
!= null
&& fast
.next
!= null
){
slow
= slow
.next
;
fast
= fast
.next
.next
;
}
return slow
;
}
}
转载请注明原文地址: https://yun.8miu.com/read-56465.html