-
Notifications
You must be signed in to change notification settings - Fork 0
206. Reverse Linked List #12
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
syoshida20
wants to merge
5
commits into
main
Choose a base branch
from
feature/stack/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
5 commits
Select commit
Hold shift + click to select a range
6366cea
feat : #12 add the empty markdown file for creating a PR
syoshida20 a7b19a0
feat : #12 finish the STEP1/STEP2/STEP3
syoshida20 9a08912
feat : #12 add 2 more solutions
syoshida20 0caf4c1
feat : #12 read the other member's PRs
syoshida20 5a7edfe
feat : #12 add the javascript instruction to markdown
syoshida20 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,197 @@ | ||
| # 206. Reverse Linked List | ||
|
|
||
| ## STEP 1 | ||
|
|
||
| * 手作業でやってみる | ||
| * 工場のライン作業を逆転させると仮定する。 | ||
| * 先頭の人から以下の作業を更新する。 | ||
| * 自分のむき先を前の人から後ろの人に向ける。 | ||
|
|
||
| ```javascript | ||
| const reverseList = function(head) { | ||
| let current = head | ||
| let previous = null | ||
| while (current !== null) { | ||
| const next = current.next | ||
| current.next = previous | ||
| previous = current | ||
| current = next | ||
| } | ||
| return previous | ||
| }; | ||
| ``` | ||
|
|
||
| * 再帰でやってみる。 | ||
| * 先頭の人から以下の作業を行う。 | ||
| * 自分の後ろの人全員の向き先を変えてもらう。(再帰) | ||
| * 自分のむき先を前の人にする。 | ||
| * 一番後ろの人を次の人から受け取り、返す。 | ||
|
|
||
| ```javascript | ||
| const reverseList = function(current, previous = null) { | ||
| if (current === null) return previous | ||
| const new_head_node = reverseList(current.next, current) | ||
| current.next = previous | ||
| return new_head_node | ||
| }; | ||
| ``` | ||
|
|
||
| ## STEP 2 | ||
|
|
||
| * やったこと | ||
| * コメントをつける。 | ||
|
|
||
| ```javascript | ||
| const reverseList = function(head) { | ||
| let current = head | ||
| let previous = null | ||
|
|
||
| while (current !== null) { | ||
| // 次の人を保持しておく。 | ||
| const next = current.next | ||
|
|
||
| // 自分の向き先を前の人に向ける。 | ||
| current.next = previous | ||
|
|
||
| previous = current | ||
| current = next | ||
| } | ||
|
|
||
| return previous | ||
| } | ||
| ``` | ||
|
|
||
| ## STEP 3 | ||
|
|
||
| ```javascript | ||
| const reverseList = function(head) { | ||
| let current = head | ||
| let previous = null | ||
| while (current !== null) { | ||
| const next = current.next | ||
| current.next = previous | ||
| previous = current | ||
| current = next | ||
| } | ||
| return previous | ||
| }; | ||
| ``` | ||
|
|
||
| ## 感想 | ||
|
|
||
| ### コメント集を読んで | ||
|
|
||
| * stackの初めは特殊なので、番兵を立てるか、分岐をするか、ループの外に出すかの選択を意識する。 | ||
|
|
||
| * ループの中で、事前に保証されていることを意識する。 | ||
| * スタックを使う際に、1つ目のループでnextを破壊しておく方法がある。 https://github.com/quinn-sasha/leetcode/pull/7/files/4ddfeff795ae1b5137089b3d59b9ff97da8409c4#r1948254138 | ||
|
|
||
| ### 他の人のコードを読んで | ||
|
|
||
| * goto-untrapped のPR https://github.com/goto-untrapped/Arai60/pull/27/ | ||
| * 再帰を行う際には、次の人に、何を渡して、何を返してもらうかを考える。 | ||
| * 先頭から順番に付け替える方法で、再帰を使う方法もある (`*4`) | ||
| https://github.com/goto-untrapped/Arai60/pull/27/files#r1641789968 | ||
|
|
||
| * fhiyoのPR https://github.com/fhiyo/leetcode/pull/7/ | ||
| * while文の主人公をnextにする方法もある (`*1`) | ||
| * stackを使う方法もある。 (`*2`) | ||
|
|
||
| * olsen-blue https://github.com/olsen-blue/Arai60/pull/7/ | ||
| * > 細かい遷移ステップを考えると「prev」や「current」などが思いつきやすいものの、これは「ループ中のある一瞬の状況」を「コードを書く人の目線」で切り取っているに過ぎず、「(初めて)コードを見る人」の視点でコード全体を俯瞰して眺めたときに意味を成さない、と自戒を込めて、理解。 | ||
| 自分が意識できていなかった。 | ||
|
|
||
| * hayashi-ay のコード https://github.com/hayashi-ay/leetcode/pull/13/ | ||
| * 大変読みやすかった。 | ||
|
|
||
| ## その他の解法 | ||
|
|
||
| * `*1` while文の主役をnextにする方法 | ||
|
|
||
| ```javascript | ||
| const reverseList = function(head) { | ||
| let current = head | ||
| if (current === null) return current | ||
| let next = current.next | ||
|
|
||
| // あらかじめ初期化しておく. | ||
| current.next = null | ||
|
|
||
| while (next) { | ||
| const twoAhead = next.next | ||
| next.next = current | ||
| current = next | ||
| next = twoAhead | ||
| } | ||
| return current | ||
| }; | ||
| ``` | ||
|
|
||
| * `*2` スタックを使って、一番後ろから順番に向き先を変更する方法 | ||
|
|
||
| ```javascript | ||
| const reverseList = function(head) { | ||
| if (head === null) { | ||
| return null | ||
| } | ||
| const stack = [] | ||
| let node = head | ||
| while (node !== null) { | ||
| stack.push(node) | ||
| node = node.next | ||
| } | ||
|
|
||
| const new_head = stack.pop() | ||
| let current = new_head | ||
| while (stack.length > 0) { | ||
| const next = stack.pop() | ||
| current.next = next | ||
| current = next | ||
| } | ||
| // While文では、もともとheadだったノードの向き先を変えられないため。 | ||
| head.next = null | ||
| return new_head | ||
| }; | ||
| ``` | ||
|
|
||
| * `*4` 再帰を用いて、先頭から順番に向き先を付け替える | ||
|
|
||
| ```javascript | ||
| const reverseListHelper = function(new_chain_head, old_chain_head) { | ||
| if (old_chain_head === null) return new_chain_head | ||
| const old_chain_new_head = old_chain_head.next | ||
| old_chain_head.next = new_chain_head | ||
| return reverseListHelper(old_chain_head, old_chain_new_head) | ||
| }; | ||
| const reverseList = function(head) { | ||
| return reverseListHelper(null, head) | ||
| } | ||
| ``` | ||
| ### レビューを受けて | ||
|
|
||
| * 変更前 | ||
|
|
||
| ```javascript | ||
| const reverseList = function(current, previous = null) { | ||
| if (current === null) return previous | ||
| const new_head_node = reverseList(current.next, current) | ||
| current.next = previous | ||
| return new_head_node | ||
| }; | ||
| ``` | ||
|
|
||
| * 変更後 (「解体中の先頭」「構築中の先頭」が素直な名前の付け方というコメント集を見て) | ||
|
|
||
| ```javascript | ||
| const reverseListHelper = function(old_chain_current, old_chain_previous = null) { | ||
| if (old_chain_current === null) return old_chain_previous | ||
| const old_chain_next = old_chain_current.next | ||
| const new_chain_head = reverseListHelper(old_chain_next, old_chain_current) | ||
| old_chain_current.next = old_chain_previous | ||
| return new_chain_head | ||
| }; | ||
| const reverseList = function(head) { | ||
| return reverseListHelper(head) | ||
| } | ||
| ``` | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
LeetCode側でのfunction callに
previous = nullという値を追加しているようですが、function signatureを変えてしまうのは問題設定を変えてしまっているような気がしてやや抵抗感があります。どこまで許されるかは面接官・業務ならコード・チーム次第だと思いますが、別解でやられているように別途helper functionを作る方が無難かなと感じます。が、私の感覚が間違っている可能性があるので、他の方々のご意見を伺いたいところです 👀
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.
JavaScript の呼び出しは順番によるもののみで、Python Ruby にあるようなキーワード引数がありません。
このため、名前を書き換えても問題はないはずです。
Python は問題があるかもしれませんね。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.wqduubixdpe4
Uh oh!
There was an error while loading. Please reload this page.
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.
Javascriptのドキュメントを読んだところ、キーワード引数に関する記載はなく、順序の記載のみでした。
参考 : https://developer.mozilla.org/en-US/docs/Glossary/Argument
また、Destructuring objects as function parametersという方法で、
キーワード引数に似たことはできるそうです。
参考: https://stackoverflow.com/questions/11796093/is-there-a-way-to-provide-named-parameters-in-a-function-call