方法:用两个指针,一个慢指针一次走一步,一个快指针每次走两步,当快指针走完时,慢指针所在节点就是中间节点,这个方法只需要遍历一遍单链表,时间复杂度为O(n);
public ListNode
middleNode(){
ListNode fast
=this.head
;
ListNode slow
=this.head
;
while(fast
!=null
&&fast
.next
!=null
){
fast
=fast
.next
.next
;
slow
=slow
.next
;
}
return slow
;
}
转载请注明原文地址: https://mac.8miu.com/read-509045.html