Skip to content

Add 206. Reverse Linked List.md #7

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 3 commits into
base: main
Choose a base branch
from
Open

Add 206. Reverse Linked List.md #7

wants to merge 3 commits into from

Conversation

t0hsumi
Copy link
Owner

@t0hsumi t0hsumi commented Dec 9, 2024

tail = dummy_head
while stack:
node = stack[-1]
stack.pop()

Choose a reason for hiding this comment

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

node = stack.pop()という書き方もあるなと思いました。

```

inner functionのtype annotaionはpython3.10+のものとした。
public APIとかではないのでつける必要はない気がした。
Copy link

Choose a reason for hiding this comment

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

拝見して、僕も気になったので調べてみました。
public APIsだと必須だけど、それ以外は必要に応じてって感じなんですね。。難しい

At least annotate your public APIs.

https://google.github.io/styleguide/pyguide.html#3191-general-rules

Copy link

Choose a reason for hiding this comment

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

最近では、ある程度大規模な開発になると型を書いているように思いますね。

- time complexity: O(n)
- space complexity: O(n) (auxiliary space: O(1))

previousを返すのが少し気持ち悪い。nodeに注目していたのに、返すものがpreviousであることに不整合を感じる。
Copy link

Choose a reason for hiding this comment

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

仕事をループの途中で引き継いだとしましょう。

これが node と previous ね、という書き置きがあったら何言っているんだって感じじゃないですか。

先頭からひっくり返していって「まだひっくり返していない部分」と「もうひっくり返した部分」という引き継ぎになるでしょう。

node は、まだ一番注目しているくらいの意味でいいでしょうが、previous は、意味ではなくて操作の順番から付いている名前ですね。 意味と形式のどちらから名前を付けるかは状況次第で、なるべくパズルを作らないようにつけましょう。

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

Choose a reason for hiding this comment

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

自分はstackにNoneを入れるのが気になりました。反転した後の末尾のnextをNoneにするためだと思うのですが、その意図を汲むのが難しいように思います。素直に2つ目のwhileを抜けた後にNoneを代入するので良いかなと思いました。

class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
def reverse_list_helper(
head: ListNode | None,
Copy link

Choose a reason for hiding this comment

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

Optional[ListNode] のほうがよく見ますかね。

return None, None
if head.next is None:
return head, head
reversed_head, reversed_tail = reverse_list_helper(head.next)
Copy link

Choose a reason for hiding this comment

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

先に head.next を別の変数に入れて、head.next = None にしてから再帰したほうが、概念的には分かりやすい気がします。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます。

reversed_tailにheadを取り付ける際に、head.next = Noneとするのも、reversed_tailを先に進めてからreversed_tail.next = Noneとするのも、少し違和感を感じていたので、この説明かなり腑に落ちました。

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.

5 participants