-
Notifications
You must be signed in to change notification settings - Fork 0
206 reverse linked list #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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,125 @@ | ||
| # Step1 | ||
|
|
||
| ## アプローチ | ||
|
|
||
| * 数字を順番に見ていってリストに内容を保存. リストの後ろを見ながら新しいノードを作成していく | ||
| * 前から順番に繋ぎ変えていく. | ||
| * in-placeでも, 別のノードリストを用意してもできる. | ||
| * 再帰で実装もできるけど本質的にはやること一緒. | ||
|
|
||
| ## Code1-1 (別のリストに数字を保存) | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None: | ||
| return None | ||
| values = [] | ||
| while head is not None: | ||
| values.append(head.val) | ||
| head = head.next | ||
| reversed_head_dummy = ListNode() | ||
| reversed_tail = reversed_head_dummy | ||
| for i in range(len(values) - 1, -1, -1): | ||
| node = ListNode(values[i]) | ||
| reversed_tail.next = node | ||
| reversed_tail = reversed_tail.next | ||
| return reversed_head_dummy.next | ||
| ``` | ||
|
|
||
| ## Code1-2 (別のメモリ使用. one path) | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None: | ||
| return None | ||
| reversed_head = None | ||
| node = head | ||
| while node is not None: | ||
| new_node = ListNode(node.val) | ||
| new_node.next = reversed_head | ||
| reversed_head = new_node | ||
| node = node.next | ||
| return reversed_head | ||
| ``` | ||
|
|
||
| ## Code1-3 (in-place, one path) | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None: | ||
| return None | ||
| reversed_head = None | ||
| node = head | ||
| while node is not None: | ||
| next_node = node.next | ||
| node.next = reversed_head | ||
| reversed_head = node | ||
| node = next_node | ||
| return reversed_head | ||
| ``` | ||
|
|
||
| # Step2 | ||
|
|
||
| 変更なし | ||
|
|
||
| # Step3 | ||
|
|
||
| ## Code1-1 (別のリストに数字を保存) | ||
|
|
||
| * `values[::-1]`とするよりも, `range(len(values) - 1, -1, -1)`を使った方が, 配列が新たにコピーされなくて済むので効率的かと思ったが, 結局`range()`も新しく配列を新たに作成する上, 中に含まれるのはどちらも`int`型だから変わらない気がしてきた. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. range は Python 3 から generator を作るので、効率的ではあります。reversed も generator ですので values[i] でしかアクセスしないならば、reversed(values) も手です。 速度の面ではコピーしてしまったほうが速い可能性はあります。 いずれにしても、速さを問題にするならば、定量的に見積もって、本当に重要なのかを考えたいです。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. generatorを呼ぶ場合は初回にpythonframeを作るのでその分の時間がかかりそうですね。 今回は速さ的な意味だとそこまで気にする必要はない程度の差だと感じました。 一方で、リストが長くなる時はメモリの効率性の観点からgeneratorを返すものを使った方が良いと思いました。 |
||
|
|
||
| ```python | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None: | ||
| return None | ||
| values = [] | ||
| node = head | ||
| while node is not None: | ||
| values.append(node.val) | ||
| node = node.next | ||
| reversed_dummy_head = ListNode() | ||
| reversed_tail = reversed_dummy_head | ||
| for i in range(len(values) - 1, -1, -1): | ||
| new_node = ListNode(values[i]) | ||
| reversed_tail.next = new_node | ||
| reversed_tail = reversed_tail.next | ||
| return reversed_dummy_head.next | ||
| ``` | ||
|
|
||
| ## Code1-2 (別のメモリ使用. one path) | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None: | ||
| return None | ||
| node = head | ||
| reversed_head = None | ||
| while node is not None: | ||
| new_node = ListNode(node.val) | ||
| new_node.next = reversed_head | ||
| reversed_head = new_node | ||
| node = node.next | ||
| return reversed_head | ||
| ``` | ||
|
|
||
| ## Code1-3 (in-place, one path) | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None: | ||
| return None | ||
| prev = None | ||
| node = head | ||
| while node is not None: | ||
| next_node = node.next | ||
| node.next = prev | ||
| prev = node | ||
| node = next_node | ||
| return prev | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ``` | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
出題意図は
stackではないにしろ,stackを使うという発想に至れば良かったdxxsxsxkx/leetcode#7 (comment)
[追記]
listの場合は
reversedも選択肢に入れるkazizi55/coding-challenges#7 (comment)