GuilinDev

Lc0876

05 August 2008

876 Middle of the Linked List

找linkedlist的中点,注意是返回中点以及以后的结点,如果总共偶数个则返回两个中点中的第二个中点及以后

快慢指针

1
2
3
4
5
6
7
8
public ListNode middleNode(ListNode head) {
    ListNode slow = head, fast = head;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow;
}