Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <vector>

#include <catch2/catch_test_macros.hpp>

#include "solution.hpp"

namespace {

leetcode::p0083::ListNode* fromVector(const std::vector<int>& 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<int> toVector(leetcode::p0083::ListNode* head) {
std::vector<int> 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<int>{1, 2});
CHECK(toVector(leetcode::p0083::solve(fromVector({1, 1, 2, 3, 3}))) ==
std::vector<int>{1, 2, 3});
CHECK(toVector(leetcode::p0083::solve(fromVector({}))).empty());
}
41 changes: 0 additions & 41 deletions typescript/deleteDuplicates.ts

This file was deleted.

Loading