Conversation
| sorted_head = checking_node | ||
| checking_node = next_node | ||
| return sorted_head | ||
| ``` |
There was a problem hiding this comment.
あとは付け替えていく方法とスタックで解く方法がありますね。
Ryotaro25/leetcode_first60#65 (comment)
There was a problem hiding this comment.
ありがとうございます。
stackを使う方法を書いてみました。(STEP2でstackのコードを読んではいました。)
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
stack = []
while head is not None:
stack.append(head)
head = head.next
stack[-1].next = None
reversed_list_head = ListNode()
reversed_list_tail = reversed_list_head
while stack:
reversed_list_tail.next = stack.pop()
reversed_list_tail = reversed_list_tail.next
return reversed_list_head.nextSTEP3で書いたものが付け替えだと思っていたのですが、ほかにありましたっけ?新規にnodeを作る方法でしょうか?
There was a problem hiding this comment.
すみません、こちら付け替えですね。
付け替え方にも3種類くらいあり勘違いしました!
https://discord.com/channels/1084280443945353267/1355246975309844550/1355898121460252784
今回は③ですかね。
There was a problem hiding this comment.
付け替え方で3種類もあるんですね。共有いただきありがとうございます。
| sorted_head = checking_node | ||
| checking_node = next_node | ||
| return sorted_head | ||
| ``` |
There was a problem hiding this comment.
prevをなくしたことで何をやっているのかが明確になっていてとても良いと思うのですが、
sortedがどこから出てきたのか気になりました。reversed_head、reliked_head等の方が伝わりやすい気がします。
There was a problem hiding this comment.
ご指摘の通りsortedはおかしいですね。reversed_headが良さそうです。ありがとうございます。
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| checking_node = head | ||
| sorted_head = None |
There was a problem hiding this comment.
reversedと書いたつもりでした。ご確認いただきありがとうございます。
This Problem
206. Reverse Linked List
Next Ploblem
703. Kth Largest Element in a Stream
言語: Python