{"id":9522,"date":"2022-03-02T06:51:51","date_gmt":"2022-03-02T14:51:51","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9522"},"modified":"2022-03-02T06:52:52","modified_gmt":"2022-03-02T14:52:52","slug":"leetcode-2181-merge-nodes-in-between-zeros","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-2181-merge-nodes-in-between-zeros\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2181. Merge Nodes in Between Zeros"},"content":{"rendered":"\n<p>You are given the&nbsp;<code>head<\/code>&nbsp;of a linked list, which contains a series of integers&nbsp;<strong>separated<\/strong>&nbsp;by&nbsp;<code>0<\/code>&#8216;s. The&nbsp;<strong>beginning<\/strong>&nbsp;and&nbsp;<strong>end<\/strong>&nbsp;of the linked list will have&nbsp;<code>Node.val == 0<\/code>.<\/p>\n\n\n\n<p>For&nbsp;<strong>every&nbsp;<\/strong>two consecutive&nbsp;<code>0<\/code>&#8216;s,&nbsp;<strong>merge<\/strong>&nbsp;all the nodes lying in between them into a single node whose value is the&nbsp;<strong>sum<\/strong>&nbsp;of all the merged nodes. The modified list should not contain any&nbsp;<code>0<\/code>&#8216;s.<\/p>\n\n\n\n<p>Return&nbsp;<em>the<\/em>&nbsp;<code>head<\/code>&nbsp;<em>of the modified linked list<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/02\/02\/ex1-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [0,3,1,0,4,5,2,0]\n<strong>Output:<\/strong> [4,11]\n<strong>Explanation:<\/strong> \nThe above figure represents the given linked list. The modified list contains\n- The sum of the nodes marked in green: 3 + 1 = 4.\n- The sum of the nodes marked in red: 4 + 5 + 2 = 11.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/02\/02\/ex2-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [0,1,0,3,0,2,2,0]\n<strong>Output:<\/strong> [1,3,4]\n<strong>Explanation:<\/strong> \nThe above figure represents the given linked list. The modified list contains\n- The sum of the nodes marked in green: 1 = 1.\n- The sum of the nodes marked in red: 3 = 3.\n- The sum of the nodes marked in yellow: 2 + 2 = 4.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in the list is in the range&nbsp;<code>[3, 2 * 10<sup>5<\/sup>]<\/code>.<\/li><li><code>0 &lt;= Node.val &lt;= 1000<\/code><\/li><li>There are&nbsp;<strong>no<\/strong>&nbsp;two consecutive nodes with&nbsp;<code>Node.val == 0<\/code>.<\/li><li>The&nbsp;<strong>beginning<\/strong>&nbsp;and&nbsp;<strong>end<\/strong>&nbsp;of the linked list have&nbsp;<code>Node.val == 0<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: List<\/strong><\/h2>\n\n\n\n<p>Skip the first zero, replace every zero node with the sum of values of its previous nodes.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  ListNode* mergeNodes(ListNode* head) {\n    ListNode dummy;    \n    head = head->next;\n    for (ListNode* prev = &dummy; head; head = head->next) {\n      int sum = 0;\n      while (head->val != 0) {\n        sum += head->val;\n        head = head->next;\n      }\n      prev->next = head;\n      head->val = sum;\n      prev = head;      \n    }\n    return dummy.next;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given the&nbsp;head&nbsp;of a linked list, which contains a series of integers&nbsp;separated&nbsp;by&nbsp;0&#8216;s. The&nbsp;beginning&nbsp;and&nbsp;end&nbsp;of the linked list will have&nbsp;Node.val == 0. For&nbsp;every&nbsp;two consecutive&nbsp;0&#8216;s,&nbsp;merge&nbsp;all the nodes&#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":[83,177,129,767],"class_list":["post-9522","post","type-post","status-publish","format-standard","hentry","category-list","tag-list","tag-medium","tag-merge","tag-skip","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9522","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=9522"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9522\/revisions"}],"predecessor-version":[{"id":9525,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9522\/revisions\/9525"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}