Skip to content
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
92 changes: 92 additions & 0 deletions 206. Reverse Linked List.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# step1
- 入力をスタックに入れていって、popしていけば良さそう

```python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
node = head
reverse_node = ListNode()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

reverseのような動詞は変数名に使うことは多くないと思います。reversed_nodeはどうでしょうか?

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.

reverseのような動詞は変数名に使うことは多くないと思います。reversed_nodeはどうでしょうか?

あまり意識できていなかったので変数考える際の参考にします

dummy= reverse_node
stack = []
while node is not None:
stack.append(node.val)
node = node.next
while stack:
reverse_node.next = ListNode(stack.pop())
Copy link
Copy Markdown

@h-masder h-masder May 18, 2026

Choose a reason for hiding this comment

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

stack.pop() のように、値を返すだけでなく状態も変更する副作用のある処理は、引数に直接書かないほうがよさそうです。コードの流れが追いづらくなったり、デバッグしにくくなったりすると思います。

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.

確かにそうですね、分けて書くようにします。ありがとうございます

reverse_node = reverse_node.next
return dummy.next
Copy link
Copy Markdown

@h-masder h-masder May 18, 2026

Choose a reason for hiding this comment

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

使用する直前に変数を宣言すると目の移動が少なくて良いと思います。

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        node = head
        stack = []
        while node is not None:
            stack.append(node.val)
            node = node.next

        reverse_node = ListNode()
        dummy= reverse_node
        while stack:
            reverse_node.next = ListNode(stack.pop())
            reverse_node = reverse_node.next
        return dummy.next

```

# step2
- PRなどを見る
- スタックではなく繋ぎ変える方法もある(出題意図としてはこっち?)
- https://github.com/olsen-blue/Arai60/pull/7/changes#r1904334523
- このPRのリンク先の繋ぎ変えの読んでコードを追ってみたが結構短いのに時間がかかってしまった
- 紙に書いて考えた時にnode.next = lastFixed_nodeでnode.nextを切り離して書いて、nodeを更新していなかった。
- 紙の端っこの少スペースで書いていたので、変数の値を同じ個所で斜線で消して上書きしていた。そのため途中で今の状態がよくわからなくなったし、どこでミスしたのかのふりかえりもしにくくなった
- コードの流れは分かったが、なぜこれで出来るのかが腹落ちしていない感覚
Copy link
Copy Markdown

@h-masder h-masder May 18, 2026

Choose a reason for hiding this comment

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

処理を空行を区切るとイメージしやすくなるかもしれません。また、コメントアウトも有効です。

        while node:
            not_fixed_node = node.next
            # リンクノードをつなぎ変える
            node.next = last_fixed_node
            last_fixed_node = node

            node = not_fixed_node

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.

ありがとうございます、紙面に書いたときは変数の動きなどを見ていて、言語化をしていなかったので、コメントアウトなどで言語化もしてみようと思います

- https://discord.com/channels/1084280443945353267/1355246975309844550/1355898121460252784
- これの3のイメージかな、コードから具体的なイメージが書けるようにしたい
- https://peps.python.org/pep-0008/#descriptive-naming-styles
- PEP8では関数名と変数名は小文字で単語間はアンダースコアでつなげる
- 再帰のやり方もある、が今回は軽く目を通す程度にしておいて次に進む
- https://discord.com/channels/1084280443945353267/1231966485610758196/1239417493211320382

```python
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
node = head
last_fixed_node = None

while node:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここはwhile node is not Noneと書いたほうがよいと思います。
このあたりご参考にしてみてください。

https://peps.python.org/pep-0008/?utm_source=chatgpt.com#programming-recommendations

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.

ありがとうございます。明確な使い分けができていなかったため、stackの方の書き方につられていました。

以下の整理をしました。

https://peps.python.org/pep-0008/?utm_source=chatgpt.com#programming-recommendations

while nodeの場合,nodeが「None」と「空」である場合にfalseと判定されてしまうので明示的に is not Noneと書く。

Also, beware of writing if x when you really mean if x is not None – e.g. when testing whether a variable or argument that defaults to None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context!

リストなどの場合以下のように書く。

For sequences, (strings, lists, tuples), use the fact that empty sequences are false:

Correct:

if not seq:
if seq:

not_fixed_node = node.next
node.next = last_fixed_node
lastFixed_node = node
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここは、last_fixed_nodeかなと思います

node = notFixed_node

return lastFixed_node
```

# step3

```python
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
node = head
last_fixed_node = None
while node:
not_fixed_node = node.next
node.next = last_fixed_node
last_fixed_node = node
node = not_fixed_node
return last_fixed_node
```
```python
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
node = head
last_fixed_node = None
while node:
not_fixed_node = node.next
node.next = last_fixed_node
last_fixed_node = node
node = not_fixed_node
return last_fixed_node
```
```python
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
node = head
last_fixed_node = None
while node:
not_fixed_node = node.next
node.next = last_fixed_node
last_fixed_node = node
node = not_fixed_node
return last_fixed_node
```