Skip to content

Create ReverseLinkedList.md#7

Open
kt-from-j wants to merge 1 commit intomainfrom
ReverseLinkedList
Open

Create ReverseLinkedList.md#7
kt-from-j wants to merge 1 commit intomainfrom
ReverseLinkedList

Conversation

@kt-from-j
Copy link
Owner

今回解いた問題:
206. Reverse Linked List
次に解く問題:
1. Two Sum
(Heap, PriorityQueueより馴染みのあるhashMap系を先に進める)

ListNode node = head;
ListNode reversedHead = null;
while(node != null) {
ListNode next = node.next;
Copy link

Choose a reason for hiding this comment

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

普段Rubyだとnextが予約語なのでギョッとしましたがJavaなので特に問題なさそうですね。
読みやすいです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

JavaでいうcontinueをRubyではnextと書くんですね。知らなかったです。
Ruby話者などをギョッとさせる可能性があることを心に留めておきます。

Copy link

Choose a reason for hiding this comment

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

Pythonでも予約語ではないですがnextという組込み関数がありますね。
他言語で使われる名前はどこまで気にすべきかはちょっと分からないですが、ご参考まで。

Comment on lines +174 to +189
class Solution {
public ListNode reverseList(ListNode head) {
return reverseListHelper(head);
}

private ListNode reverseListHelper(ListNode node) {
if (node == null || node.next == null) {
return node;
}
ListNode headReversed = reverseListHelper(node.next);
ListNode tailReversed = node.next;
tailReversed.next = node;
node.next = null;
return headReversed;
}
}
Copy link

Choose a reason for hiding this comment

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

好みの問題かもしれませんが、先にheadがnullの場合を返しておけば再帰の条件が減らせますね。

class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null) return head;

        return reverseListHelper(head);
    }

  private ListNode reverseListHelper(ListNode node) {
        if (node.next == null) return node;

        ListNode headReversed = reverseListHelper(node.next);
        ListNode tailReversed = node.next;
        tailReversed.next = node;
        node.next = null;
        return headReversed;
    }
}

Copy link
Owner Author

@kt-from-j kt-from-j Sep 30, 2025

Choose a reason for hiding this comment

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

確かにそちらの方が自然な気がします。ありがとうございます!

}
```

# step2 他の方の解答を見る

Choose a reason for hiding this comment

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

変数名の修正などされていて良いと思います。
網羅的にしすぎると何時間も掛かってしまう(キリがない)ドツボにハマると思うので、要点など整理して主要な内容をreferenceと共にstep2にまとめるのがいいと思いました。
*私はドツボにハマりしばらくかけてないです。

Copy link

Choose a reason for hiding this comment

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

あ、これ、理事の一人は60問を6時間かからずに解き切ったそうです。
これは平均よりも速いとは思いますが、最終的にはそれくらいのオーダー感覚になるものです。
100%絞り出そうとせずに次に行くのも大事です。

要はですね、大人ならば5分くらいで書けるメールを、状況に合わせて60通書いてみようくらいの話なのですね。
日本語ネイティブだが、日本で義務教育を受けておらず文字やタイピングに慣れていないという人がいると、それが1時間以上かかるわけです。

同じようなメールを書く必要になったときに「何も見ずに5分程度で頭から尻尾まで書いて、軽く見直しをし、社会人として恥ずかしくない程度ができあがる」能力がつけばゴールであり、60通書き終わった時点でそうなっていればいいわけです。ここで、お悔やみのメール全パターンを列挙し分類しようとか始めると文化人類学の研究にはなるかもしれませんが目的が違いそうです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

なんとなくこの回で選択肢の幅が見えてなかったことに気づいたように感じました。
気づいたからには網羅的と思い、しばし手が止まってしまいました。
1問1問完璧ではなくても進めていく中で見えていくものも多いかと思うので、こだわり過ぎず進めていこうと思います。

Copy link

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.

その辺りを意識して取り組んでみます。
ありがとうございます。

Copy link

@nanae772 nanae772 left a comment

Choose a reason for hiding this comment

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

お疲れ様です。いろんな解き方を丁寧に実装されていて読みやすかったです。

- Stackを使って実装
- Stackを使わなくても書けそうな気がするが、どうしてStackのセクションにあるのだろう。
- 当初、末尾の要素のnextをnullにするのを忘れていた。
- 最後の要素だけが問題になるので、ループ完了後に`newTail.next = null;`としているが、ループの中で上書きした方が読み手にとって自然だろうか。
Copy link

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.

やっぱりループ完了後に上書きしていると唐突な印象を受けますよね。
コメントありがとうございます!

```
- https://github.com/akmhmgc/arai60/pull/7/files
- 1ノードずつ逆順のLinked Listの先頭に繋いでいく
- 上の実装とやっていることはほぼ同じだが、すっきりした実装に
Copy link

Choose a reason for hiding this comment

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

上の実装は破壊的でこちらは非破壊という違いもありますね

ListNode node = head;
ListNode reversedHead = null;
while(node != null) {
ListNode next = node.next;
Copy link

Choose a reason for hiding this comment

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

Pythonでも予約語ではないですがnextという組込み関数がありますね。
他言語で使われる名前はどこまで気にすべきかはちょっと分からないですが、ご参考まで。

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.

5 participants