{"id":3351,"date":"2018-07-28T21:35:52","date_gmt":"2018-07-29T04:35:52","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3351"},"modified":"2018-07-28T21:36:05","modified_gmt":"2018-07-29T04:36:05","slug":"leetcode-876-middle-of-the-linked-list","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-876-middle-of-the-linked-list\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 876. Middle of the Linked List"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Given a non-empty, singly\u00a0linked list with head node\u00a0<code>head<\/code>, return\u00a0a\u00a0middle node of linked list.<\/p>\n<p>If there are two middle nodes, return the second middle node.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[1,2,3,4,5]<\/span>\r\n<strong>Output: <\/strong>Node 3 from this list (Serialization: <span id=\"example-output-1\">[3,4,5]<\/span>)\r\nThe returned node has value 3.  (The judge's serialization of this node is [3,4,5]).\r\nNote that we returned a ListNode object ans, such that:\r\nans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong><span id=\"example-input-2-1\">[1,2,3,4,5,6]<\/span>\r\n<strong>Output: <\/strong>Node 4 from this list (Serialization: <span id=\"example-output-2\">[4,5,6]<\/span>)\r\nSince the list has two middle nodes with values 3 and 4, we return the second one.\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>The number of nodes in the given list will be between\u00a0<code>1<\/code>\u00a0and\u00a0<code>100<\/code>.<\/li>\n<\/ul>\n<h1><strong>Solution: Slow + Fast Pointers<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 0 ms\r\nclass Solution {\r\npublic:\r\n  ListNode* middleNode(ListNode* head) {\r\n    if (!head || !head-&gt;next) return head;\r\n    auto slow = head;\r\n    auto fast = head;\r\n    while (fast &amp;&amp; fast-&gt;next) {\r\n      slow = slow-&gt;next;\r\n      fast = fast-&gt;next-&gt;next;\r\n    }\r\n    return slow;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a non-empty, singly\u00a0linked list with head node\u00a0head, return\u00a0a\u00a0middle node of linked list. If there are two middle nodes, return the second middle node.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[222,83,358],"class_list":["post-3351","post","type-post","status-publish","format-standard","hentry","category-list","tag-easy","tag-list","tag-middle","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3351"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3351\/revisions"}],"predecessor-version":[{"id":3353,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3351\/revisions\/3353"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}