206. Reversed Linked List#8
Open
tNita wants to merge 1 commit into
Open
Conversation
oda
reviewed
Dec 27, 2025
| return reverse_helper(head)[0] | ||
| ``` | ||
|
|
||
| 2-1) 実はtailは不要(上でnode.next is Noneの時にnode, nodeと二つ同じものを返しているところに違和感を感じて気づく?) |
| ``` | ||
| - https://github.com/resumit30minutes/leetcode-arai60-practice/pull/8/files#diff-56cce9af784f5c572f0706e03269e28af28f8aabbface21c32e6199d6400c657 | ||
| - https://github.com/quinn-sasha/leetcode/pull/7#discussion_r1948355100 | ||
| - stackを利用する場合でもNodeのまま次へのつながりを切ってstackに積むやり方もあり |
There was a problem hiding this comment.
一応、この問題の出題意図は、LinkedList でお手玉ができますかということかと思うので、これは出題意図には沿っていないだろうと推測します。
mamo3gr
reviewed
Dec 28, 2025
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| reversed_head = None | ||
| while head is not None: | ||
| reversed_head = ListNode(head.val, reversed_head) |
There was a problem hiding this comment.
ListNode を作ると結構シンプルに書けるんですね。この場合はもとのノードが残る(新しいノードが増える)ので、そこの良し悪しを意識できると良いと思いました。
nodchip
reviewed
Dec 29, 2025
| return None | ||
|
|
||
| node = head | ||
| stack = deque() |
There was a problem hiding this comment.
スタックとして使うのであれば list で十分だと思います。一般に、同じ目的に使えるデータ構造が複数ある場合、その中で一番実装が軽いものを選ぶことが多いように思います。
| ```Python3 | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| original_head = head |
There was a problem hiding this comment.
head はリンクトリストの先頭を表すノードのため、変数名を変えたほうが良いかもしれません。ただ、今回は head の位置のノードを逆順のリンクトリストに繋ぎ変え、 head が残りの部分の先頭を表すノードになるため、そのままでも十分通じるかもしれません。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
この問題:https://leetcode.com/problems/reverse-linked-list
次の問題:https://leetcode.com/problems/kth-largest-element-in-a-stream