LeetCode 876. Middle of the Linked List (intermediate node list)

Topic Tags: Linked List

  Let's find the middle point of the title, you can use the pointer speed, specifically to see the code.

 

Java Solution:

Runtime: 0 ms, faster than 100% 

Memory Usage: 33MB, less than 100%

Completion Date: 05/04/2019

Key point: the speed of the pointer

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        
        return slow;
    }
}

References: N / A

LeetCode title list -  LeetCode Questions List

Topic Source: https: //leetcode.com/

Guess you like

Origin www.cnblogs.com/jimmycheng/p/11136519.html