{"id":110,"date":"2021-08-02T12:29:48","date_gmt":"2021-08-02T04:29:48","guid":{"rendered":"http:\/\/dmoe.nicolite.cn\/?p=110"},"modified":"2021-08-02T12:33:32","modified_gmt":"2021-08-02T04:33:32","slug":"leetcode1171","status":"publish","type":"post","link":"https:\/\/dmoe.nicolite.cn\/?p=110","title":{"rendered":"LeetCode1171"},"content":{"rendered":"\n<p>\u5730\u5740\uff1a<a href=\"https:\/\/leetcode-cn.com\/problems\/remove-zero-sum-consecutive-nodes-from-linked-list\/\" target=\"_blank\" rel=\"noreferrer noopener\">1171. \u4ece\u94fe\u8868\u4e2d\u5220\u53bb\u603b\u548c\u503c\u4e3a\u96f6\u7684\u8fde\u7eed\u8282\u70b9<\/a><\/p>\n\n\n\n<p><strong>\u8bed\u8a00\uff1aJava<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n *\/\nclass Solution {\n    public ListNode removeZeroSumSublists(ListNode head) {\n        ListNode result = new ListNode(0); \/\/\u5904\u7406&#91;0]\u3001&#91;0,0]\u3001&#91;1,-1]\u5c11\u4e8e3\u4e2a\u8282\u70b9\u7684\u60c5\u51b5\n        result.next = head;\n\n        HashMap&lt;Integer, ListNode&gt; hmap = new HashMap&lt;&gt;();\n        int sum = 0;\n        \/\/\u5efa\u7acbsum &lt;-&gt; ListNode\u6620\u5c04\n        for (ListNode temp = result; temp != null; temp = temp.next) {\n            sum += temp.val;\n            hmap.put(sum, temp);\n        }\n\n        sum = 0;\n        \/\/\u5982\u679c\u8fde\u7eed\u533a\u95f4\u5185\u6709\u4e24\u4e2a\u8282\u70b9\u7684\u524d\u7f00\u548c\u662f\u76f8\u540c\u7684\uff0c\u90a3\u4e48\u533a\u95f4\u7684\u548c\u4e3a0\n        for (ListNode temp = result; temp != null; temp = temp.next) {\n            sum += temp.val;\n            if(hmap.containsKey(sum)) {\n                temp.next = hmap.get(sum).next;\n            }\n        }\n\n        return result.next;\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8bed\u8a00 Golang\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for singly-linked list.\n * type ListNode struct {\n *     Val int\n *     Next *ListNode\n * }\n *\/\nfunc removeZeroSumSublists(head *ListNode) *ListNode {\n    var mapx = make(map&#91;int]*ListNode, 0)\n    var r *ListNode = &amp;ListNode{}\n    r.Next = head\n    var sum = 0\n    for temp:= r; temp != nil; temp = temp.Next {\n        sum += temp.Val\n        mapx&#91;sum] = temp\n    }\n\n    sum = 0;\n    for temp := r; temp != nil; temp = temp.Next  {\n          sum += temp.Val\n          ele, ok := mapx&#91;sum]\n          if ok {\n              temp.Next = ele.Next\n          }\n    }\n\n    return r.Next\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5730\u5740\uff1a1171. \u4ece\u94fe\u8868\u4e2d\u5220\u53bb\u603b\u548c\u503c\u4e3a\u96f6\u7684\u8fde\u7eed\u8282\u70b9 \u8bed\u8a00\uff1aJava \u8bed\u8a00 Golang\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,15],"tags":[17,18],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-leetcode","category-15","tag-leetcode","tag-18"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"_links":{"self":[{"href":"https:\/\/dmoe.nicolite.cn\/index.php?rest_route=\/wp\/v2\/posts\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dmoe.nicolite.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dmoe.nicolite.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dmoe.nicolite.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dmoe.nicolite.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=110"}],"version-history":[{"count":0,"href":"https:\/\/dmoe.nicolite.cn\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions"}],"wp:attachment":[{"href":"https:\/\/dmoe.nicolite.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dmoe.nicolite.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dmoe.nicolite.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}