LeetCode #1171

Remove Zero Sum Consecutive Nodes from Linked List

1개의 풀이 · C++

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeZeroSumSublists(ListNode* head) {
        unordered_map<int, ListNode*> mp;
        int S = 0;
        ListNode *dummy = new ListNode(0, head);
        ListNode *cur = dummy;
        while(cur) {
            S += cur->val;
            mp[S] = cur;
            cur = cur->next;
        }
        cur = dummy; S = 0;
        while(cur) {
            S += cur->val;
            cur->next = mp[S]->next;
            cur = cur->next;
        }
        return dummy->next;
    }
};

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.