Skip to content

Wanwan87 206. reverse linked list#7

Open
wanwan87 wants to merge 2 commits into
mainfrom
wanwan87-206.-Reverse-Linked-List
Open

Wanwan87 206. reverse linked list#7
wanwan87 wants to merge 2 commits into
mainfrom
wanwan87-206.-Reverse-Linked-List

Conversation

@wanwan87
Copy link
Copy Markdown
Owner

while stack:
reverse_node.next = ListNode(stack.pop())
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

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.

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

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はどうでしょうか?

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

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:

while node:
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かなと思います

- この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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants