876 , 链表的中间节点

https://leetcode.cn/problems/middle-of-the-linked-list/

 

使用快慢指针

 1 lass Solution {
 2     public ListNode middleNode(ListNode head) {
 3         // 排除为空情况
 4         if(head == null){
 5             return null;
 6         }
 7         // 使用快慢指针
 8         ListNode slow = head;
 9         ListNode fast = head;
10 
11         while(fast != null && fast.next != null){
12             slow = slow.next;
13             fast = fast.next.next;
14         }
15         return slow;
16     }
17 }

 

posted @ 2023-08-28 22:13  陆陆无为而治者  阅读(7)  评论(0编辑  收藏  举报