Skip to content

Create 206. Reverse Linked List.md#7

Open
tokuhirat wants to merge 1 commit intomainfrom
206.-Reverse-Linked-List
Open

Create 206. Reverse Linked List.md#7
tokuhirat wants to merge 1 commit intomainfrom
206.-Reverse-Linked-List

Conversation

@tokuhirat
Copy link
Copy Markdown
Owner

This Problem
206. Reverse Linked List
Next Ploblem
703. Kth Largest Element in a Stream
言語: Python

sorted_head = checking_node
checking_node = next_node
return sorted_head
```
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

読みやすいです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あとは付け替えていく方法とスタックで解く方法がありますね。
Ryotaro25/leetcode_first60#65 (comment)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
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.next

STEP3で書いたものが付け替えだと思っていたのですが、ほかにありましたっけ?新規にnodeを作る方法でしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

すみません、こちら付け替えですね。
付け替え方にも3種類くらいあり勘違いしました!
https://discord.com/channels/1084280443945353267/1355246975309844550/1355898121460252784
今回は③ですかね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

付け替え方で3種類もあるんですね。共有いただきありがとうございます。

sorted_head = checking_node
checking_node = next_node
return sorted_head
```
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

prevをなくしたことで何をやっているのかが明確になっていてとても良いと思うのですが、
sortedがどこから出てきたのか気になりました。reversed_headreliked_head等の方が伝わりやすい気がします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ご指摘の通りsortedはおかしいですね。reversed_headが良さそうです。ありがとうございます。

class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
checking_node = head
sorted_head = None
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sorted は reversed でしょうか。

特に問題ないと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

reversedと書いたつもりでした。ご確認いただきありがとうございます。

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