LeetCode 876. Middle of the Linked List (链表的中间结点)

题目标签:Linked List

  题目让我们找到中间点,可以利用快慢指针,具体看code。

 

Java Solution:

Runtime: 0 ms, faster than 100% 

Memory Usage: 33MB, less than 100%

完成日期:05/04/2019

关键点:快慢指针

/**
 * 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;
    }
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

posted @ 2019-07-05 09:53  Jimmy_Cheng  阅读(194)  评论(0编辑  收藏  举报