Conversation
arai60/add-two-numbers/step2.py
Outdated
| l1 = l1.next | ||
| l2 = l2.next |
There was a problem hiding this comment.
削除、コミット、 git push で修正できると思います。レビューワーの負担を下げるため、修正してからレビュー依頼を頂けるとありがたかったです。
|
|
||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| head = ListNode(-1) |
There was a problem hiding this comment.
head は、現在注目しているノード、リストの先端のノードを表すことが多いと思います。 dummy または sentinel はいかがでしょうか?
There was a problem hiding this comment.
ご指摘ありがとうございます。
dummyを使おうと思います!
arai60/add-two-numbers/step2.py
Outdated
| l1 = l1.next | ||
| l2 = l2.next |
There was a problem hiding this comment.
削除、コミット、 git push で修正できると思います。レビューワーの負担を下げるため、修正してからレビュー依頼を頂けるとありがたかったです。
|
|
||
| c = 0 | ||
| while l1 and l2: | ||
| s = (l1.val + l2.val + c)%10 |
There was a problem hiding this comment.
% の両隣にスペースを空けたほうが良いと思います。
PEP8 では、演算子の優先順位の異なる複数の演算子を1 つの式で用いる場合、順位の低い演算子の両隣にスペースを空けることを推奨しています。
https://peps.python.org/pep-0008/#other-recommendations
Google Python Style Guide では、適切に判断してください、としています。
https://google.github.io/styleguide/pyguide.html#36-whitespace
PEP8 と Google Python Style Guide を一通り読まれることをお勧めします。
| carry = 0 | ||
| while l1 or l2 or carry: | ||
| sum_val = carry | ||
| if l1: |
There was a problem hiding this comment.
sum_val += l1.val if l1 else 0 という書き方もあります。 l1 = l1.next if l1 else None と組み合わせると if 文が消せます。ただ、これで読みやすくなっているかと言われると、微妙なところです。
arai60/add-two-numbers/step4.py
Outdated
| if l2: | ||
| sum_val += l2.val | ||
| l2 = l2.next | ||
| carry = sum_val//10 |
https://leetcode.com/problems/add-two-numbers/description/