-
Notifications
You must be signed in to change notification settings - Fork 0
92. Reversed Linked List 2 #46
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?
Conversation
| - current, next, prevなどの多用は、操作上の問題で、あまり意図が見えないので好みではない(と言いつつ自分もちょこちょこ使っているが) | ||
| - odaさんの2番目のコードで書いてみた。区間の左端と、その1個まえのnodeがどこにつながっているかというのだけ変わっているが、実質そんな変わらない気がする | ||
| - まあ左端の直前のnodeが、左端のnodeからどんどん奪っていく、という見方をすればちょっと気持ちは違うか | ||
| - odaさんのコード見たらinsert_afterだけreturn NoneのNoneがないけど、なんか意図があるのかな |
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.
返り値の型の意図が None か Optional[ListNode] かですね。
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.
確かにremove_afterはnodeを返しませんね。(ありがとうございます)
| node = node.next | ||
| return node | ||
|
|
||
| def remove_after(self, node): |
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.
remove_afterだと後ろのすべてのnodeを消しそうに感じました。
remove_next、pop_nextとかどうでしょうか。
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.
これ、確かに〜となりました。(ありがとうございます)
| reversed_head = before_reversed.next | ||
| for i in range(right - left): | ||
| removed = self.remove_after(reversed_head) | ||
| self.insert_after(before_reversed, removed) |
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.
nodeの入れ替えをよく混乱していまうのですが、このように分けると理解しやすいですね
| ### Step2 | ||
|
|
||
| - https://discord.com/channels/1084280443945353267/1196498607977799853/1354841302826619021 | ||
| - torusさんの。なるほど、1 passじゃないとそう解くのか |
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.
逆に私は1-passの方法がパッと浮かびませんでした。
確かnodeの並び順を逆にする、みたいな問題があったのを覚えていて、それと組み合わせて解こう(その問題のフォローアップみたいな位置付け)という感じで考えました。
問題文: https://leetcode.com/problems/reverse-linked-list-ii/description/
Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.