Skip to content

206. Reverse Linked List#7

Open
hemispherium wants to merge 1 commit intomainfrom
0206-reverse-linked-list
Open

206. Reverse Linked List#7
hemispherium wants to merge 1 commit intomainfrom
0206-reverse-linked-list

Conversation

@hemispherium
Copy link
Owner

@hemispherium hemispherium self-assigned this Dec 12, 2025
Comment on lines +14 to +26
stack<int> st;
while (head != nullptr) {
st.push(head->val);
head = head->next;
}
ListNode* dummy = new ListNode(0);
ListNode* curr = dummy;
while (!st.empty()) {
int num = st.top(); st.pop();
curr->next = new ListNode(num);
curr = curr->next;
}
return dummy->next;
Copy link

Choose a reason for hiding this comment

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

変数名(st,curr)について、個人的には省略する理由がなければ、省略せずにそのままstack,currentにすると思いました。未来の自分を含めた他の人が読む時に、頭の中で元の単語に戻すという作業をさせない方が楽だなという気持ちです。

以下、nodchipさんのコメントを引用します。

英単語から文字を削って識別子とした場合、読み手に取って認知負荷が上がる場合があります。原則フルスペルで書くことをおすすめします。情報科学やソフトウェアエンジニアリングにおいて有名な略語 (API, LAN, DNS, JSON 等) や、所属するチーム内で頻繁に使われている略語は、使用しても問題ないと思います。また、

    number of を表す num_
    sum of を表す sum_
    maximum number of を表す max_
    minimum number of を表す min_

などは、しばしば見かけますので、使ってもよいと思います。

参考までにスタイルガイドへのリンクを貼ります。

https://google.github.io/styleguide/pyguide.html#316-naming

    Avoid abbreviation. In particular, do not use abbreviations that are ambiguous or unfamiliar to readers outside your project, and do not abbreviate by deleting letters within a word.

上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。

ListNode* dummy = new ListNode(0);
ListNode* curr = dummy;
while (!st.empty()) {
int num = st.top(); st.pop();
Copy link

Choose a reason for hiding this comment

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

C++言語の慣習、文化的なものから来るコードスタイルではない場合、ここは2行に分けて書くのが一般的かなと思いました。

Suggested change
int num = st.top(); st.pop();
int num = st.top();
st.pop();

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.

2 participants