-
Notifications
You must be signed in to change notification settings - Fork 0
206. Reverse Linked List #8
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
| if (!head || !head->next) return head; | ||
|
|
||
| ListNode* new_head = reverseList(head->next); | ||
| head->next->next = head; |
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.
私、head->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.
なるほど。確かに返り値で次に処理すべきノードを返してあげれば脳の負担が減ります。
この考え方でも実装してみます。
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.
こちらに実装してみました。-> 0ce1690
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.
| ListNode* previous_node = nullptr; | ||
| ListNode* node = head; | ||
| while (node) { | ||
| ListNode* temp = node->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.
tempよりnext_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.
一時的な保存に使っているのでtempとしていました。見直してみます。
問題へのリンク
https://leetcode.com/problems/reverse-linked-list/description/
次に解く問題
703. Kth Largest Element in a Stream
README.mdへ頭の中の言語化と記録をしています。