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,113 @@
// data_structures

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) {}
};

// Step1
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (head == nullptr)
return nullptr;

ListNode *node_head = head;

if (node_head->next != nullptr && node_head->val == node_head->next->val) {
while (node_head->next != nullptr && node_head->val == node_head->next->val) {
node_head = node_head->next;
}
node_head = node_head->next;
}
if (node_head == nullptr)
return nullptr;

ListNode *node = node_head;
ListNode *node_scanning = node_head->next;

while (node_scanning != nullptr) {
if (node->val == node_scanning->val) {
node_scanning = node_scanning->next;
} else {
node = node_scanning;
node_scanning = node_scanning->next;
}
}
}
};

// Step2
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (head == nullptr)
return nullptr;

ListNode dummy(0, head);
ListNode *unique = &dummy;
ListNode *node = head;

while (node != nullptr) {
if (node->next != nullptr && node->val == node->next->val) {
while (node->next != nullptr && node->val == node->next->val) {
node = node->next;
}
unique->next = node->next;
} else {
unique = unique->next;
}
node = node->next;
}
return dummy.next;
}
};

// Step3 リファクタリング
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (head == nullptr || head->next == nullptr) return head;

ListNode dummy(0, head);
ListNode *unique = &dummy;
ListNode *node = head;

while (node != nullptr) {
if (node->next != nullptr && node->val == node->next->val) {
int dupVal = node->val;
while (node != nullptr && node->val == dupVal) {
node = node->next;
}
unique->next = node;
} else {
node = node->next;
unique = unique->next;
}
}
return dummy.next;
Comment on lines +79 to +91
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rinta-Rinta/LeetCode_arai60#3 (comment)
https://discord.com/channels/1084280443945353267/1195700948786491403/1196701558382018590

処理が分岐する際に、処理の長さや簡単さによってはif-elseで並べずにcontinueやbreakで抜ける方が、
分かりやすい場合もありそうです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、大事なご指摘だと思います。
特にシンプルな処理の場合は、先にcontinueで抜けた方が可読性が上がる場面も多いと感じました。
ありがとうございます🙏

}
};

//Step4 再帰
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (head == nullptr || head->next == nullptr) {
return head;
}

if (head->val == head->next->val) {
while (head->next != nullptr && head->val == head->next->val) {
head = head->next;
}
return deleteDuplicates(head->next);
} else {
head->next = deleteDuplicates(head->next);
return head;
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# 82. Remove Duplicates from Sorted List II
- [Arai60](https://1kohei1.com/leetcode/)
- 問題文: [82. Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)
- 使用言語: C++
- 次に解く問題: [2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/)

## 知識セット
### 知っていたこと
- 連結リストのポインタの扱い方
- 再帰の書き方

### 今回調べたこと
- コーナーケースとエッジケースの違い
- エッジケース: 境界値などを入れたとき
- エッジケース: 複数のエッジケースや環境が特殊な組み合わせの時
- dummy node(番兵ノード)
- early return
- 実際のプログラミングではdeleteする、したい

## Step1
- 所要時間: 30min.
- 方針: 現在のvalと一個前のポインタを保管し、valの重複があったらskip、変わっていたらポインタからnextを付け替える
- headが重複している場合のエッジケースの考慮が必要
- head一つのみのときの処理が必要
Comment on lines +23 to +24
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この方法でもできるはずですが、確かに大変です。Step 2の方法で、重複していない先頭が見つかるまでは別のループを前につけるか、そうでなければ、保存するノードが見つかったときに、先頭が見つかっているかいなかで条件分岐します。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、番兵を用意するか条件分岐で実現できますね。ありがとうございます。
愚直にも実装できるようにはしておきたいと思います。

- 実装を試みたもののロジックが複雑になりすぎたので途中で断念
- コード
```cpp
public:
ListNode* deleteDuplicates(ListNode* head) {
if (head == nullptr)
return nullptr;

ListNode *node_head = head;

if (node_head->next != nullptr && node_head->val == node_head->next->val) {
while (node_head->next != nullptr && node_head->val == node_head->next->val) {
node_head = node_head->next;
}
node_head = node_head->next;
}
if (node_head == nullptr)
return nullptr;

ListNode *node = node_head;
ListNode *node_scanning = node_head->next;

while (node_scanning != nullptr) {
if (node->val == node_scanning->val) {
node_scanning = node_scanning->next;
} else {
node = node_scanning;
node_scanning = node_scanning->next;
}
}
}
```

## Step2
- 所要時間: 20min.
- 方針: ダミーノードを用意することでheadもその後のnodeも同じロジックで扱える
- 重複がない安全なnodeをポインタとして保存
- 重複を検知して、その塊を丸ごとskipする
- コード
```cpp
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (head == nullptr)
return nullptr;

ListNode dummy(0, head);
ListNode *unique = &dummy;
ListNode *node = head;

while (node != nullptr) {
if (node->next != nullptr && node->val == node->next->val) {
while (node->next != nullptr && node->val == node->next->val) {
node = node->next;
}
unique->next = node->next;
} else {
unique = unique->next;
}
node = node->next;
}
return dummy.next;
}
};
```

## Step3 リファクタリング
- 所要時間: 15min
- 方針:
- 重複検知をdupValという変数を介することで意図をわかりやすくした
- early return を1行にしてみた
- 最後にnodeを進めるのをそれぞれの処理に分割した
- コード
```cpp
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (head == nullptr || head->next == nullptr) return head;

ListNode dummy(0, head);
ListNode *unique = &dummy;
ListNode *node = head;

while (node != nullptr) {
if (node->next != nullptr && node->val == node->next->val) {
int dupVal = node->val;
while (node != nullptr && node->val == dupVal) {
node = node->next;
}
unique->next = node;
} else {
node = node->next;
unique = unique->next;
}
}
return dummy.next;
}
};
```

## Step4 再帰
- 所要時間: 15min
- 方針: 再帰を使うとシンプルに書けるらしいので、やってみる
- ベースケースは最後のnullptr
- 基本的には固まりごとに処理
- 重複があれば重複をskipして値が変わったら再帰
- 重複がなければそこは確定して次から再帰
- コード
```cpp
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (head == nullptr || head->next == nullptr) {
return head;
}

if (head->val == head->next->val) {
while (head->next != nullptr && head->val == head->next->val) {
head = head->next;
}
return deleteDuplicates(head->next);
} else {
head->next = deleteDuplicates(head->next);
return head;
}
}
};
```