Skip to content

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
102 changes: 102 additions & 0 deletions 206. Reverse Linked List_new.md
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()まではできていたので悔しい。
Copy link

Choose a reason for hiding this comment

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

これ、stack の中で1つ目だけが特殊なのです。
なので、番兵を立てるか、分岐をするか、ループの外に出すかの選択になります。どれでもいいので発想できれば書けたでしょう。

あと、stack の中のノードがこの時点ではまだ .next で全部繋がっていることも意識しましょう。

araiさんはこの問題をstackに分類していたので、これが想定解なのかな。
Copy link

Choose a reason for hiding this comment

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

再帰を用いた解法もあるのでこれも見てみる。
```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ポインタを消すため。