-
Notifications
You must be signed in to change notification settings - Fork 0
206. Reverse Linked List_new.md #10
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
lilnoahhh
wants to merge
2
commits into
main
Choose a base branch
from
lilnoahhh-patch-8-1
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
2 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,102 @@ | ||
206. Reverse Linked List | ||
思考順に書く | ||
人の発言の引用は「」 | ||
|
||
step1 思いつかず | ||
stackを使うんだろうなとは思ったが単連結リストをstackに入れると後ろの繋がった部分まで入ってしまう。 | ||
連結の繋ぎ目を一旦消す→リストとしてstackに入れていく→popして単連結リストとして吐き出すという流れで解こうとした。 | ||
|
||
CodingNinjaさんの動画を見た。(https://www.youtube.com/watch?v=9TsQmdRAxT8) | ||
かなりシンプルで驚いた。他の人のコードも見ていく。 | ||
|
||
(https://github.com/tarinaihitori/leetcode/pull/6/) | ||
めっちゃ勉強になる。以下はスタックでの解法 | ||
```python | ||
class Solution: | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
stack = [] | ||
current = head | ||
while current: | ||
stack.append(current) | ||
current = current.next | ||
if not stack: | ||
return None | ||
reversed_head = stack.pop() | ||
current = reversed_head | ||
while stack: | ||
current.next = stack.pop() | ||
current = current.next | ||
current.next = None | ||
return reversed_head | ||
``` | ||
これreversed_head=stack.pop()まではできていたので悔しい。 | ||
araiさんはこの問題をstackに分類していたので、これが想定解なのかな。 | ||
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. 私はこれは linked list の繋ぎ変えだと思いますね。 |
||
再帰を用いた解法もあるのでこれも見てみる。 | ||
```python | ||
class Solution: | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
if head is None or head.next is None: | ||
return head | ||
new_head = self.reverseList(head.next) | ||
head.next.next = head | ||
head.next = None | ||
return new_head | ||
``` | ||
正直今の自分の実力的に再現できなさそう。 | ||
これはarai60一週した後にできるようになればいっか。 | ||
|
||
スタックのやり方でいく。 | ||
step2 | ||
```python | ||
class Solution: | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
stack = [] | ||
node = head | ||
|
||
while node: | ||
stack.append(node) | ||
node = node.next | ||
if not stack: | ||
return None | ||
reversed_list = stack.pop() | ||
reversed_node = reversed_list | ||
while stack: | ||
reversed_node.next = stack.pop() | ||
reversed_node = reversed_node.next | ||
reversed_node.next = None | ||
return reversed_list | ||
``` | ||
変数名は自分が読みやすいと思うように変更した。 | ||
|
||
step3 | ||
1回目 3分11 | ||
2回目 2分10 | ||
3回目 1分50 | ||
```python | ||
class Solution: | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
stack = [] | ||
node = head | ||
|
||
while node: | ||
stack.append(node) | ||
node = node.next | ||
if not stack: | ||
return None | ||
|
||
reversed_list = stack.pop() | ||
reversed_node = reversed_list | ||
|
||
while stack: | ||
reversed_node.next = stack.pop() | ||
reversed_node = reversed_node.next | ||
reversed_node.next = None | ||
return reversed_list | ||
``` | ||
|
||
勘違いしていたことがあったので追加して書く。 | ||
|
||
連結リストをstackに入れても連結リスト全体が入るわけではない。 | ||
たとえば1->2->3という連結リストがあり、これをstackに入れていくと各ノードつまり1、2、3という順に入れていくことになる。 | ||
ただしnextポインタはstackに入れても残っていることに注意! | ||
reversedのときに最後のノードのnextをNoneにしているのは、このnextポインタを消すため。 |
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.
これ、stack の中で1つ目だけが特殊なのです。
なので、番兵を立てるか、分岐をするか、ループの外に出すかの選択になります。どれでもいいので発想できれば書けたでしょう。
あと、stack の中のノードがこの時点ではまだ .next で全部繋がっていることも意識しましょう。