Skip to content

Conversation

@kazukiii
Copy link
Owner

問題へのリンク
https://leetcode.com/problems/reverse-linked-list/description/

次に解く問題
703. Kth Largest Element in a Stream

README.mdへ頭の中の言語化と記録をしています。

if (!head || !head->next) return head;

ListNode* new_head = reverseList(head->next);
head->next->next = head;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私、head->next がお尻になることを考えさせないほうが負荷が軽いと思うんですよね。どこかでその議論をしていたはずです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど。確かに返り値で次に処理すべきノードを返してあげれば脳の負担が減ります。
この考え方でも実装してみます。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらに実装してみました。-> 0ce1690

Copy link

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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tempよりnext_nodeとかの方が良さそうです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一時的な保存に使っているのでtempとしていました。見直してみます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants