{"id":2477,"date":"2018-04-12T22:23:44","date_gmt":"2018-04-13T05:23:44","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2477"},"modified":"2018-04-12T22:23:57","modified_gmt":"2018-04-13T05:23:57","slug":"leetcode-328-odd-even-linked-list","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-328-odd-even-linked-list\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 328. Odd Even Linked List"},"content":{"rendered":"<div class=\"question-description\">\n<div>\n<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u628a\u6240\u6709\u5947\u6570\u4f4d\u7f6e\u7684\u8282\u70b9\u4e32\u5728\u4e00\u8d77\uff0c\u540e\u9762\u8ddf\u7740\u6240\u6709\u4e32\u5728\u4e00\u8d77\u7684\u5076\u6570\u4f4d\u7f6e\u7684\u8282\u70b9\u3002<\/p>\n<p>Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.<\/p>\n<p>You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.<\/p>\n<p><b>Example:<\/b><br \/>\nGiven\u00a0<code>1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL<\/code>,<br \/>\nreturn\u00a0<code>1-&gt;3-&gt;5-&gt;2-&gt;4-&gt;NULL<\/code>.<\/p>\n<p><b>Note:<\/b><br \/>\nThe relative order inside both the even and odd groups should remain as it was in the input.<br \/>\nThe first node is considered odd, the second node even and so on &#8230;<\/p>\n<p><b>Credits:<\/b><br \/>\nSpecial thanks to\u00a0<a href=\"https:\/\/leetcode.com\/discuss\/user\/DjangoUnchained\">@DjangoUnchained<\/a>\u00a0for adding this problem and creating all test cases.<\/p>\n<\/div>\n<\/div>\n<h1>Solution<\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 21 ms\r\nclass Solution {\r\npublic:\r\n  ListNode* oddEvenList(ListNode* head) {\r\n    if (head == nullptr) return head;\r\n    ListNode dummy_odd(0);\r\n    ListNode dummy_even(0);\r\n    ListNode* prev_odd = &amp;dummy_odd;\r\n    ListNode* prev_even = &amp;dummy_even;\r\n    int index = 0;\r\n    while (head) {      \r\n      auto next = head-&gt;next;\r\n      head-&gt;next = nullptr; \/\/ important\r\n      if (index++ &amp; 1) {\r\n        prev_even-&gt;next = head;\r\n        prev_even = head;\r\n      } else {\r\n        prev_odd-&gt;next = head;\r\n        prev_odd = head;\r\n      }\r\n      head = next;\r\n    }\r\n    prev_odd-&gt;next = dummy_even.next;\r\n    return dummy_odd.next;\r\n  }\r\n};<\/pre>\n<p>V2<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 18 ms\r\nclass Solution {\r\npublic:\r\n  ListNode* oddEvenList(ListNode* head) {\r\n    if (head == nullptr) return head;\r\n    vector&lt;ListNode&gt; heads(2, ListNode(0));    \r\n    vector&lt;ListNode*&gt; prevs{&amp;heads[0], &amp;heads[1]};\r\n    int index = 0;\r\n    while (head) {\r\n      auto next = head-&gt;next;\r\n      head-&gt;next = nullptr; \/\/ important      \r\n      prevs[index]-&gt;next = head;\r\n      prevs[index] = head;\r\n      head = next;\r\n      index ^= 1;\r\n    }\r\n    prevs[0]-&gt;next = heads[1].next;\r\n    return heads[0].next;\r\n  }\r\n};<\/pre>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 1 ms\r\nclass Solution {\r\n  public ListNode oddEvenList(ListNode head) {\r\n    ListNode[] heads = new ListNode[]{new ListNode(0), new ListNode(0)};\r\n    ListNode[] prevs = new ListNode[]{heads[0], heads[1]};\r\n    int index = 0;\r\n    while (head != null) {\r\n      ListNode next = head.next;\r\n      head.next = null;\r\n      prevs[index].next = head;\r\n      prevs[index] = prevs[index].next;\r\n      head = next;\r\n      index ^= 1;\r\n    }\r\n    prevs[0].next = heads[1].next;\r\n    return heads[0].next;\r\n  }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true\">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 56 ms\r\n\"\"\"\r\nclass Solution:\r\n  def oddEvenList(self, head):\r\n    heads = [ListNode(0), ListNode(0)]\r\n    prevs = [heads[0], heads[1]]\r\n    index = 0\r\n    while head:\r\n      nxt = head.next\r\n      head.next = None\r\n      prevs[index].next = head\r\n      prevs[index] = prevs[index].next\r\n      head = nxt\r\n      index ^= 1\r\n    prevs[0].next = heads[1].next\r\n    return heads[0].next<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u628a\u6240\u6709\u5947\u6570\u4f4d\u7f6e\u7684\u8282\u70b9\u4e32\u5728\u4e00\u8d77\uff0c\u540e\u9762\u8ddf\u7740\u6240\u6709\u4e32\u5728\u4e00\u8d77\u7684\u5076\u6570\u4f4d\u7f6e\u7684\u8282\u70b9\u3002 Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the&#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":[295,83,177],"class_list":["post-2477","post","type-post","status-publish","format-standard","hentry","category-list","tag-dummy-head","tag-list","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2477","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=2477"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2477\/revisions"}],"predecessor-version":[{"id":2479,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2477\/revisions\/2479"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}