-
Notifications
You must be signed in to change notification settings - Fork 0
206. Reverse Linked List #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
36605bd
96625c4
dcb76d6
13dcc6d
b282584
a94aeee
d453a11
16f6011
c3652c5
c1bfe40
eeace60
c223cdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # step1 | ||
|
|
||
| ループ処理の最中でノードを反転させていく方法が一番最初に思いついた。(というか、多分やったことがあって記憶にあった) | ||
| 2分くらいで手順を書き出して、それをコード化することができた。 | ||
|
|
||
| # step2 | ||
|
|
||
| ループでのアプローチ以外に | ||
| - 再帰 | ||
| - スタック | ||
| があることを知った。 | ||
|
|
||
| ## 再帰について | ||
|
|
||
| 今回再帰でコード書いてみると通りはするけど、実際にノードの連結数に明記がないので不適。(LeetCodeの環境なら大丈夫なコードになってしまうから) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 問題文の制約に
とありました。またLeetcodeではrecursionlimitが550000に設定されているようです。 |
||
|
|
||
| 時間計算量はO(N), 空間計算量はO(N) | ||
|
|
||
| ## スタック | ||
|
|
||
| アプローチはとても自然で他人が書いたコードでも読みやすいと思った。 | ||
| ただ空間計算量がO(N)になり、他のアプローチを考えるとデメリット。 | ||
|
|
||
| 時間計算量はO(N), 空間計算量はO(N) | ||
|
|
||
| ## step3 | ||
|
|
||
| AIもつかってリファクタリングをした。 | ||
| 再帰はベースケースとエッジケースを最初に明記して後続の処理をよりシンプルに書くように変更 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| prev = None | ||
| node = head | ||
|
|
||
| while node: | ||
| tmp = node.next | ||
| node.next = prev | ||
| prev = node | ||
| node = tmp | ||
| return prev | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| previous_node, current_node = None, head | ||
|
|
||
| while current_node: | ||
| tmp = current_node.next | ||
| current_node.next = previous_node | ||
| previous_node = current_node | ||
| current_node = tmp | ||
| return previous_node | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if not head: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 個人的には |
||
| return None | ||
|
|
||
| new_head = head | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この部分の役割がぴんとこず、上のbase caseにまとめたほうがすっきりしそうだなと思いましたが実際step3ではそのようにされていてそちらのほうが意図が取りやすかったです。 |
||
|
|
||
| if head.next: | ||
| new_head = self.reverseList(head.next) | ||
| head.next.next = head | ||
| head.next = None | ||
|
|
||
| return new_head | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if not head: | ||
| return None | ||
|
|
||
| stack = [] | ||
| node = head | ||
| while node: | ||
| stack.append(node) | ||
| node = node.next | ||
|
|
||
| new_head = stack.pop() | ||
| tail = new_head | ||
|
|
||
| while stack: | ||
| new_tail = stack.pop() | ||
| tail.next = new_tail | ||
| tail = tail.next | ||
|
|
||
| tail.next = None | ||
| return new_head | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| previous_node, current_node = None, head | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| while current_node: | ||
| tmp = current_node.next | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. スコープはかなり狭いので問題無いかなという気もしつつ、基本的にtmpという名前は情報が無いので少し避けたい気持ちがあります。 |
||
| current_node.next = previous_node | ||
| previous_node = current_node | ||
| current_node = tmp | ||
| return previous_node | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None or head.next is None: | ||
| return head | ||
|
|
||
| new_head = self.reverseList(head.next) | ||
| head.next.next = head | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| head.next = None | ||
|
|
||
| return new_head | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None or head.next is None: | ||
| return head | ||
|
|
||
| stack = [] | ||
| node = head | ||
|
|
||
| while node: | ||
| stack.append(node) | ||
| node = node.next | ||
|
|
||
| tail = stack.pop() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tailより「逆順リストの先頭」である名前だとreturnに来た時に直感的に分かりやすいかな、と個人的に思いました There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1です。どういう意味でtailなのか明瞭でないように思います。 |
||
| node = tail | ||
|
|
||
| while stack: | ||
| node.next = stack.pop() | ||
| node = node.next | ||
| node.next = None | ||
|
Comment on lines
+19
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 最初にstackから1つ取り出しておくのではなく、
とやって全てwhileの中に処理をまとめるというのもありかなと思いました。 |
||
|
|
||
| return tail | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 細かいところで恐縮なのですがいくつかのファイルの最後に余分な空行が入っているのが少し気になりました。 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None or head.next is None: | ||
| return head | ||
|
|
||
| new_head_node, current_node = None, head | ||
|
|
||
| while current_node: | ||
| rest_node_head = current_node.next | ||
| current_node.next = new_head_node | ||
| new_head_node = current_node | ||
| current_node = rest_node_head | ||
| return new_head_node | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None or head.next is None: | ||
| return head | ||
|
|
||
| new_head = self.reverseList(head.next) | ||
| new_previous_node = head.next | ||
| new_previous_node.next = head | ||
| head.next = None | ||
| return new_head | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None or head.next is None: | ||
| return head | ||
| stack = [] | ||
| node = head | ||
| while node: | ||
| stack.append(node) | ||
| node = node.next | ||
| new_head = stack.pop() | ||
| node = new_head | ||
| while stack: | ||
| node.next = stack.pop() | ||
| node = node.next | ||
| node.next = None | ||
| return new_head |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
再帰の書き方については2種類あるようなのでそちらも見ておくと勉強になるかもしれません
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.x5w37bodndgj