{"id":1965,"date":"2016-04-10T22:38:52","date_gmt":"2016-04-10T14:38:52","guid":{"rendered":"http:\/\/boweihe.me\/?p=1965"},"modified":"2016-04-10T22:38:52","modified_gmt":"2016-04-10T14:38:52","slug":"leetcode-328-odd-even-linked-list","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1965","title":{"rendered":"LeetCode 328. Odd Even Linked List"},"content":{"rendered":"<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.<br \/>\nYou should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.<br \/>\n<b>Example:<\/b><br \/>\nGiven <code>1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL<\/code>,<br \/>\nreturn <code>1-&gt;3-&gt;5-&gt;2-&gt;4-&gt;NULL<\/code>.<br \/>\n<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<h3>\u601d\u8def<\/h3>\n<p>\u65e2\u7136\u662f\u8981O(n)\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u5e76\u4e14O(1)\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u57fa\u672c\u5c31\u80fd\u60f3\u5230\u8981\u7528\u4fe9\u6307\u9488\u4e86\u3002\u5176\u4e2d\u4e00\u4e2a\u6307\u9488\u6307\u5411\u5f53\u524d\u7684\u5947\u6570\u4f4d\u5143\u7d20\uff0c\u53e6\u4e00\u4e2a\u6307\u5411\u5076\u6570\u4f4d\u7684\u3002<br \/>\n<a href=\"http:\/\/boweihe.me\/wp-content\/uploads\/2016\/04\/LeetCode328.jpg\" rel=\"attachment wp-att-1966\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1966\" src=\"http:\/\/boweihe.me\/wp-content\/uploads\/2016\/04\/LeetCode328-300x86.jpg\" alt=\"LeetCode328\" width=\"300\" height=\"86\" \/><\/a><br \/>\n\u5927\u6982\u5c31\u662f\u8fd9\u4e2a\u610f\u601d\u5566\uff0c\u7136\u540e\u6307\u9488\u4e0d\u65ad\u5f80\u524d\u632a\u52a8\u3002\u6ce8\u610f\u8981\u4fdd\u5b58\u5076\u6570\u6307\u9488\u7684\u5934\uff0c\u56e0\u4e3a\u5947\u6570\u7ed3\u675f\u4e4b\u540e\u8981\u4e32\u4e0a\u5076\u6570\u7684~<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<p>\u91cc\u9762\u7684oddTail\u662f\u4e3a\u4e86\u9632\u6b62\u94fe\u8868\u957f\u5ea6\u4e3a\u5947\u6570\u7684\u65f6\u5019\uff0c\u91cc\u9762currOdd\u6307\u9488\u8d70\u5230NULL\u7684\u60c5\u51b5\u3002<\/p>\n<pre class=\"lang:c++ decode:true \">\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode(int x) : val(x), next(NULL) {}\n * };\n *\/\nclass Solution {\npublic:\n    ListNode* oddEvenList(ListNode* head) {\n        if(head == NULL || head-&gt;next == NULL)\n            return head;\n        ListNode* currOdd = head;\n        ListNode* currEven = head-&gt;next;\n        ListNode* evenHead = currEven;\n        ListNode* oddTail = currOdd;\n        while(currOdd != NULL &amp;&amp; currEven != NULL){\n            currOdd-&gt;next =currEven-&gt;next;\n            if(currOdd-&gt;next != NULL)\n                currEven-&gt;next = currOdd-&gt;next-&gt;next;\n            oddTail = currOdd;\n            currOdd = currOdd-&gt;next;\n            currEven = currEven-&gt;next;\n        }\n        if(currOdd != NULL)\n            currOdd-&gt;next = evenHead;\n        else\n            oddTail-&gt;next = evenHead;\n        return head;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<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. You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity. Example: Given 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,207],"class_list":["post-1965","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-207"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1965"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1965\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}