From 03098ae9a775e4588fe865033fd91029cfe2551a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:33:21 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20Port=20problem=2083=20(Remove=20?= =?UTF-8?q?Duplicates=20from=20Sorted=20List)=20from=20TypeScript=20to=20C?= =?UTF-8?q?++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces typescript/deleteDuplicates.ts with solutions/0001-0100/0083-remove-duplicates-from-sorted-list/, simplified to a single-pointer walk (the TS version split the same idea across two mutually-recursive-looking helpers). --- .../README.md | 11 +++++ .../solution.cpp | 19 +++++++++ .../solution.hpp | 16 ++++++++ .../test.cpp | 34 +++++++++++++++ typescript/deleteDuplicates.ts | 41 ------------------- 5 files changed, 80 insertions(+), 41 deletions(-) create mode 100644 solutions/0001-0100/0083-remove-duplicates-from-sorted-list/README.md create mode 100644 solutions/0001-0100/0083-remove-duplicates-from-sorted-list/solution.cpp create mode 100644 solutions/0001-0100/0083-remove-duplicates-from-sorted-list/solution.hpp create mode 100644 solutions/0001-0100/0083-remove-duplicates-from-sorted-list/test.cpp delete mode 100644 typescript/deleteDuplicates.ts diff --git a/solutions/0001-0100/0083-remove-duplicates-from-sorted-list/README.md b/solutions/0001-0100/0083-remove-duplicates-from-sorted-list/README.md new file mode 100644 index 0000000..2ad51d3 --- /dev/null +++ b/solutions/0001-0100/0083-remove-duplicates-from-sorted-list/README.md @@ -0,0 +1,11 @@ +# 83. Remove Duplicates from Sorted List + +[LeetCode problem 83](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) + +- **Difficulty**: Easy +- **Tags**: linked-list + +## Description + +Given the head of a sorted linked list, delete all duplicates so each +element appears only once, and return the linked list, still sorted. diff --git a/solutions/0001-0100/0083-remove-duplicates-from-sorted-list/solution.cpp b/solutions/0001-0100/0083-remove-duplicates-from-sorted-list/solution.cpp new file mode 100644 index 0000000..91d36ce --- /dev/null +++ b/solutions/0001-0100/0083-remove-duplicates-from-sorted-list/solution.cpp @@ -0,0 +1,19 @@ +#include "solution.hpp" + +namespace leetcode::p0083 { + +ListNode* solve(ListNode* head) { + ListNode* current = head; + + while (current != nullptr && current->next != nullptr) { + if (current->next->val == current->val) { + current->next = current->next->next; + } else { + current = current->next; + } + } + + return head; +} + +} // namespace leetcode::p0083 diff --git a/solutions/0001-0100/0083-remove-duplicates-from-sorted-list/solution.hpp b/solutions/0001-0100/0083-remove-duplicates-from-sorted-list/solution.hpp new file mode 100644 index 0000000..a13ae29 --- /dev/null +++ b/solutions/0001-0100/0083-remove-duplicates-from-sorted-list/solution.hpp @@ -0,0 +1,16 @@ +#pragma once + +namespace leetcode::p0083 { + +struct ListNode { + int val; + ListNode* next; + + explicit ListNode(int value = 0, ListNode* nextNode = nullptr) : val(value), next(nextNode) {} +}; + +// optimized: O(n) time, O(1) extra space — walk the list once, skipping any +// node whose value matches the current one. +ListNode* solve(ListNode* head); + +} // namespace leetcode::p0083 diff --git a/solutions/0001-0100/0083-remove-duplicates-from-sorted-list/test.cpp b/solutions/0001-0100/0083-remove-duplicates-from-sorted-list/test.cpp new file mode 100644 index 0000000..dffb130 --- /dev/null +++ b/solutions/0001-0100/0083-remove-duplicates-from-sorted-list/test.cpp @@ -0,0 +1,34 @@ +#include + +#include + +#include "solution.hpp" + +namespace { + +leetcode::p0083::ListNode* fromVector(const std::vector& values) { + leetcode::p0083::ListNode dummy; + leetcode::p0083::ListNode* current = &dummy; + for (int value : values) { + current->next = new leetcode::p0083::ListNode(value); + current = current->next; + } + return dummy.next; +} + +std::vector toVector(leetcode::p0083::ListNode* head) { + std::vector result; + for (auto* node = head; node != nullptr; node = node->next) { + result.push_back(node->val); + } + return result; +} + +} // namespace + +TEST_CASE("Problem 83", "[p0083]") { + CHECK(toVector(leetcode::p0083::solve(fromVector({1, 1, 2}))) == std::vector{1, 2}); + CHECK(toVector(leetcode::p0083::solve(fromVector({1, 1, 2, 3, 3}))) == + std::vector{1, 2, 3}); + CHECK(toVector(leetcode::p0083::solve(fromVector({}))).empty()); +} diff --git a/typescript/deleteDuplicates.ts b/typescript/deleteDuplicates.ts deleted file mode 100644 index dcc7f9a..0000000 --- a/typescript/deleteDuplicates.ts +++ /dev/null @@ -1,41 +0,0 @@ -class ListNode { - val: number; - next: ListNode | null; - - constructor(val?: number, next?: ListNode | null) { - this.val = val === undefined ? 0 : val; - this.next = next === undefined ? null : next; - } -} - -const auxRemoveDuplicates = (node: ListNode | null): ListNode | null => { - if (!node?.next) { - return node; - } - - const valueToRemove: number = node.val; - - while (node.next && node.next.val === valueToRemove) { - node.next = node.next.next; - } - - return node; -}; - -const deleteDuplicates = (head: ListNode | null): ListNode | null => { - if (!head?.next) { - return head; - } - - let current: ListNode | null = head; - - while (current?.next) { - if (current.val === current.next.val) { - current = auxRemoveDuplicates(current); - } else { - current = current.next; - } - } - - return head; -};