-
Notifications
You must be signed in to change notification settings - Fork 0
206. Reverse Linked List #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kazukiii
wants to merge
3
commits into
main
Choose a base branch
from
arai60/reverse-linked-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| ## 考察 | ||
| - 過去に解いたことあり | ||
| - 方針は2つ思い浮かんだ | ||
| - 再帰の帰りがけで後ろから繋ぎ変えていく | ||
| - time: O(n), space: O(n) | ||
| - stackに一旦積んで、取り出しながら後ろから繋ぎ変えていく | ||
| - time: O(n), space: O(n) | ||
| - ノードの数は最大で5000 -> スタックオーバーフローも大丈夫そうなので再帰でやってみる | ||
| - あとは実装 | ||
|
|
||
| ## Step1 | ||
| - 上のアルゴリズムを実装 | ||
| - stackを使っても考え方は一緒なのでやってみた | ||
| - time: O(n), space: O(n) | ||
|
|
||
| ## Step2 | ||
| - Solutionsを覗いてみた | ||
| - 後ろからやらなくても、前から繋ぎ変えていくこともできたみたい | ||
| - 前から繋ぎ変えていけば、再帰呼び出しのコールスタックやスタックがなくなるので、メモリ使用量がO(1)で済んだ | ||
|
|
||
| ## Step3 | ||
| - 1回目: 1m03s | ||
| - 2回目: 54s | ||
| - 3回目: 48s | ||
|
|
||
| ## Step4 | ||
| - レビューを元に修正 | ||
| - 変数名を変更 | ||
| - temp -> next_node | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * 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* reverseList(ListNode* head) { | ||
| if (!head || !head->next) return head; | ||
|
|
||
| ListNode* new_head = reverseList(head->next); | ||
| head->next->next = head; | ||
| head->next = nullptr; | ||
| return new_head; | ||
| } | ||
| }; | ||
29 changes: 29 additions & 0 deletions
29
arai60/reverse-linked-list/step1_recursion_with_helper.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /** | ||
| * 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* reverseList(ListNode* head) { | ||
| auto result = reverseListHelper(head); | ||
| return result.first; | ||
| } | ||
|
|
||
| private: | ||
| pair<ListNode*, ListNode*> reverseListHelper(ListNode* node) { | ||
| if (!node) return {nullptr, nullptr}; | ||
| if (!node->next) return {node, node}; | ||
|
|
||
| ListNode* next_node = node->next; | ||
| node->next = nullptr; | ||
| auto [head, tail] = reverseListHelper(next_node); | ||
| tail->next = node; | ||
| return {head, node}; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * 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* reverseList(ListNode* head) { | ||
| if (!head) return nullptr; | ||
|
|
||
| stack<ListNode*> node_stack; | ||
| ListNode* node = head; | ||
| while (node) { | ||
| node_stack.push(node); | ||
| node = node->next; | ||
| } | ||
|
|
||
| ListNode* new_head = node_stack.top(); | ||
| node_stack.pop(); | ||
| while (!node_stack.empty()) { | ||
| node = node_stack.top(); | ||
| node_stack.pop(); | ||
| node->next->next = node; | ||
| node->next = nullptr; | ||
| } | ||
| return new_head; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * 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* reverseList(ListNode* head) { | ||
| ListNode* previous_node = nullptr; | ||
| ListNode* node = head; | ||
| while (node) { | ||
| ListNode* temp = node->next; | ||
| node->next = previous_node; | ||
| previous_node = node; | ||
| node = temp; | ||
| } | ||
| return previous_node; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * 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* reverseList(ListNode* head) { | ||
| ListNode* previous_node = nullptr; | ||
| ListNode* node = head; | ||
| while (node) { | ||
| ListNode* temp = node->next; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tempよりnext_nodeとかの方が良さそうです。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一時的な保存に使っているのでtempとしていました。見直してみます。 |
||
| node->next = previous_node; | ||
| previous_node = node; | ||
| node = temp; | ||
| } | ||
| return previous_node; | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * 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* reverseList(ListNode* head) { | ||
| ListNode* previous_node = nullptr; | ||
| ListNode* node = head; | ||
| while (node) { | ||
| ListNode* next_node = node->next; | ||
| node->next = previous_node; | ||
| previous_node = node; | ||
| node = next_node; | ||
| } | ||
| return previous_node; | ||
| } | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
私、head->next がお尻になることを考えさせないほうが負荷が軽いと思うんですよね。どこかでその議論をしていたはずです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど。確かに返り値で次に処理すべきノードを返してあげれば脳の負担が減ります。
この考え方でも実装してみます。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらに実装してみました。-> 0ce1690
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(あとから見る人のためのメモ)
https://discord.com/channels/1084280443945353267/1231966485610758196/1239417493211320382