美文网首页
Leetcode 876. Middle of the Link

Leetcode 876. Middle of the Link

作者: 大龄码农的技术点滴 | 来源:发表于2019-07-24 12:41 被阅读0次

    Given a non-empty, singly linked list with head node head, return a middle node of linked list.
    If there are two middle nodes, return the second middle node.

    Example 1:
    Input: [1,2,3,4,5]
    Output: Node 3 from this list (Serialization: [3,4,5])
    The returned node has value 3. (The judge's serialization of this node is [3,4,5]).
    Note that we returned a ListNode object ans, such that:
    ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.

    Example 2:
    Input: [1,2,3,4,5,6]
    Output: Node 4 from this list (Serialization: [4,5,6])
    Since the list has two middle nodes with values 3 and 4, we return the second one.

    Note:
    The number of nodes in the given list will be between 1 and 100.

    题目分析:
    本题要求中间节点,那么最直接的思路就是先从头遍历该链表,统计出链表长度n,那么中间的节点就应该是第(n+1)/2个,那么在从列表头往后遍历到第(n+1)/2个就是所求的中间节点。
    虽然这种方法的时间复杂度为O(n),但是我们需要两次遍历该链表。

    一种比较方便的做法是利用快慢指针,这也是在链表的问题中比较常见的一种策略。在本题中慢指针每次前进一步,快指针每次前进两步,这样在快指针到达链表末尾的时候,慢指针就会指向中间节点。这里注意快慢指针在一开始时都应该初始化为头结点。这种做法相比于第一种直观做法,虽然时间复杂度还是O(n),但算法只需要一次列表遍历就可以了。

    对应的Golang代码如下

    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func middleNode(head *ListNode) *ListNode {
        if head == nil || head.Next == nil {
            return head
        }
        var slow *ListNode = head
        var fast *ListNode = head
        for fast != nil && fast.Next != nil {
            fast = fast.Next.Next
            slow = slow.Next
        }
        return slow
    }
    

    对应的C#代码如下

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

    对应的C++代码如下

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* middleNode(ListNode* head) {
            if (head == NULL || head->next == NULL) return head;
            ListNode *slow = head, *fast = head;
            while (fast != NULL && fast->next != NULL) {
                slow = slow->next;
                fast = fast->next;
                if (fast != NULL) fast = fast->next;
            }
            return slow;
        }
    };
    

    对应的java代码如下

    /**
     * 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, fast = head;
            while (fast != null && fast.next != null) {
                slow = slow.next;
                fast = fast.next;
                if (fast.next != null) fast = fast.next;
            }
            return slow;
        }
    }
    

    相关文章

      网友评论

          本文标题:Leetcode 876. Middle of the Link

          本文链接:https://www.haomeiwen.com/subject/zjmolctx.html