Skip to content

206. Reverse Linked List#13

Open
Yuto729 wants to merge 2 commits intomainfrom
reverse-linked-list
Open

206. Reverse Linked List#13
Yuto729 wants to merge 2 commits intomainfrom
reverse-linked-list

Conversation

@Yuto729
Copy link
Copy Markdown
Owner

@Yuto729 Yuto729 commented Nov 29, 2025

解く問題

Reverse Linked List

次に解く問題

Kth Largest Element In A Stream

@Yuto729 Yuto729 changed the title Reverse Linked List 206. Reverse Linked List Nov 29, 2025
Repository owner deleted a comment from github-actions bot Nov 29, 2025
Comment on lines +176 to +177
return new_head
if not 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.

inner function定義部分の区別がつきやすいよう、ここには改行を入れたいなと思いました。

Suggested change
return new_head
if not head:
return new_head
if not head:

if not head:
return head

head, _tail = reverseListHelper(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.

未使用変数は1つだけであり、他の未使用変数と区別する必要が無いことから単に_にすると思いました。

Suggested change
head, _tail = reverseListHelper(head)
head, _ = reverseListHelper(head)

Comment on lines +7 to +10
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
node = head
stack = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

各 node を繋げたまま stack に格納していくのに違和感があったため、自分なら以下のように、一度繋がりを切ってから stack に格納する書き方をすると思いました。

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        node = head
        stack = []
        
        while node is not None:
            next_node = node.next
            node.next = None
            stack.append(node)
            node = next_node
        
        dummy = ListNode()
        tail = dummy
        while stack:
            tail.next = stack.pop()
            tail = tail.next

        return dummy.next

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.

なるほど
このような書き方もあるのですね!参考になります。

Comment on lines +29 to +32
以下のように記述したが間違い. head.next以降を逆順に並び替えたものの後ろに加えるという発想はあっていたが, これだと末尾を返すことになってしまう.
```py
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

「末尾を返している」というよりは、頭を返してはいるものの、head を取り付ける位置が違っている、という表現の方がが正確なのかなと感じます。以下で書くと、正常に動くと思います。

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head is None or head.next is None:
            return head

        reversed_head = self.reverseList(head.next)
        
        ## head.next から逆順にすると、処理後に head.next は tail になるので、head.next の次を head にしてあげれば良い。
        head.next.next = head
        head.next = None
        return reversed_head

Comment on lines +52 to +55
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
def reverseListHelper(node):
# 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.

こちらが考え方 2の実装で、末尾再帰で書いている方が考え方 1 実装かなと思います。こちらでは、「自分が持っている部分は渡さず、自分が持っている部分から先(n番目以降)を逆順にしたものを返却してもらって」、返却してもらったものをもとに自分で処理を行なっているためです。

Comment on lines +120 to +123
def reverseListHelper(reversed_head, rest_node):
if rest_node.next is None:
rest_node.next = reversed_head
return rest_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.

reversed_head が「すでに逆順にしたノードの先頭」、rest_node が「まだ逆順にしていないノードの先頭」であることを踏まえると、rest_node.next をもとに終了を判定するのではなく、rest_node がなくなればその時点で逆順にする処理は終わっているため、そのまま reversed_head を返す方がわかりやすいのかなと思います。

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        def reverseListHelper(reversed_head, rest_node):
            if rest_node is None:
                return reversed_head
            
            rest_node_next = rest_node.next
            rest_node.next = reversed_head
            return reverseListHelper(rest_node, rest_node_next)

        if not head:
            return head

        return reverseListHelper(None, head) 

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.

3 participants