diff --git a/cpp/2203/220309.cpp b/cpp/2203/220309.cpp new file mode 100644 index 0000000..f154107 --- /dev/null +++ b/cpp/2203/220309.cpp @@ -0,0 +1,59 @@ +#include + +/** + * Definition for singly-linked list. + */ + +struct ListNode { + int val; + ListNode* next; + + explicit ListNode(int x = 0, ListNode* next = nullptr) : val(x), next(next) {} +}; + +ListNode* construct(int x) { + return new ListNode(x); +} + +template +ListNode* construct(int x, Ts... xs) { + return new ListNode(x, construct(xs...)); +} + +/** + * 82. Remove Duplicates from Sorted List II + * Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. + */ + +class Solution { +public: + static ListNode* deleteDuplicates(ListNode* head) { + ListNode ret(0, head); + ListNode* prev = &ret; + ListNode* ptr = head; + while (ptr) { + if (ptr->next && ptr->next->val == ptr->val) { + while (ptr && ptr->next && ptr->next->val == ptr->val) + ptr = ptr->next; + if (!ptr || !ptr->next) { + prev->next = nullptr; + return ret.next; + } + prev->next = ptr->next; + } else { + // prev->next = ptr; + prev = ptr; + } + ptr = ptr->next; + } + return ret.next; + } +}; + +int main() { + ListNode* h = construct(1,1,1,2,2,3,3,3,3,3); + ListNode* ret = Solution::deleteDuplicates(h); + for (ListNode* ptr = ret; ptr; ptr = ptr->next) + std::cout << ptr->val << std::endl; + return 0; +} diff --git a/cpp/2203/CMakeLists.txt b/cpp/2203/CMakeLists.txt index 35f8a5f..a38ec52 100644 --- a/cpp/2203/CMakeLists.txt +++ b/cpp/2203/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2203) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2203 220309-CN.cpp) +ADD_EXECUTABLE(2203 220309.cpp)