/**
* 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 slow = head;
while (fast != null) {
if (fast.next == null) {
fast = fast.next;
} else {
fast = fast.next.next;
slow = slow.next;
}
}
return slow;
}
}
LeetCode-876:链表的中间结点(Java语言实现)
最新推荐文章于 2024-09-14 18:52:56 发布