-
Notifications
You must be signed in to change notification settings - Fork 0
Reorder List #140
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
Open
tom4649
wants to merge
2
commits into
main
Choose a base branch
from
143.Reorder-List
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Reorder List #140
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # 143. Reorder List | ||
|
|
||
| ## step1 | ||
| まずlistを使った方法を書いてみる。5mほど。これでは解けたことにならなそうなのでポインタのみを使って計算量を抑えたい。 | ||
|
|
||
| Discussionの以下を見て解法を思いついた。後半を reverse すればいけそう。ヒントなしで解法には辿り着けなかった。 | ||
|
|
||
| > Good practice for most of the important techniques of dealing with linked lists, ie. 2 pointer traversing, reversing, merging, ... | ||
|
|
||
| ここまで17mほど。変数名を改善 | ||
|
|
||
| ## 他の人のコード | ||
|
|
||
| https://github.com/thonda28/leetcode/pull/4 | ||
|
|
||
| > ノード数が偶数のとき、真ん中のノードは、左半分の最後と右半分の最初の二つありますよね。どっちを返すかコメントに書いた方が分かりやすいです。 | ||
|
|
||
| たしかに | ||
|
|
||
| - front, back, interleaveの命名 | ||
|
|
||
| 自分のformer, latterでもよいとは思った | ||
|
|
||
|
|
||
| > 最初からなるべく(ちょうどいい粒度の)関数に分けた方がいいのではないでしょうか?普段コードを書く時も、全てのコードをメイン関数に書いてから、関数化したりしないですよね。 | ||
|
|
||
| 関数化しようと思わなかったのは反省すべきなのかもしれない | ||
|
|
||
| https://github.com/potrue/leetcode/pull/68 | ||
|
|
||
| ## step2 | ||
| ## step3 |
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
| 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 reorderList(self, head: Optional[ListNode]) -> None: | ||
| if head is None: | ||
| return | ||
|
|
||
| ordered_list = [] | ||
| node = head | ||
| while node is not None: | ||
| ordered_list.append(node) | ||
| node = node.next | ||
|
|
||
| left = 0 | ||
| right = len(ordered_list) - 1 | ||
| while left < right: | ||
| ordered_list[left].next = ordered_list[right] | ||
| left += 1 | ||
| if left == right: | ||
| break | ||
| ordered_list[right].next = ordered_list[left] | ||
| right -= 1 | ||
|
|
||
| ordered_list[left].next = None |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reorderList(self, head: Optional[ListNode]) -> None: | ||
| if head is None: | ||
| return | ||
|
|
||
| slow = head | ||
| fast = head | ||
| while fast and fast.next: | ||
| slow = slow.next | ||
| fast = fast.next.next | ||
|
|
||
| original_latter = slow.next | ||
| slow.next = None | ||
| node = None | ||
|
|
||
| while original_latter is not None: | ||
| temp = original_latter.next | ||
| original_latter.next = node | ||
| node = original_latter | ||
| original_latter = temp | ||
|
|
||
| original_former = head | ||
| original_latter = node | ||
| while original_former is not None and original_latter is not None: | ||
| next_former = original_former.next | ||
| next_latter = original_latter.next | ||
| original_former.next = original_latter | ||
| original_latter.next = next_former | ||
| original_former = next_former | ||
| original_latter = next_latter | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reorderList(self, head: Optional[ListNode]) -> None: | ||
| if head is None: | ||
| return | ||
|
|
||
| slow = head | ||
| fast = head | ||
| while fast is not None and fast.next is not None: | ||
| slow = slow.next | ||
| fast = fast.next.next | ||
|
|
||
| latter = slow.next | ||
| slow.next = None | ||
| node = None | ||
|
|
||
| while latter is not None: | ||
| next_latter = latter.next | ||
| latter.next = node | ||
| node = latter | ||
| latter = next_latter | ||
|
|
||
| former = head | ||
| latter = node | ||
| while former is not None and latter is not None: | ||
| next_former = former.next | ||
| next_latter = latter.next | ||
| former.next = latter | ||
| latter.next = next_former | ||
| former = next_former | ||
| latter = next_latter |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Definition for singly-linked list. | ||
| class ListNode: | ||
| def __init__(self, val=0, next=None): | ||
| self.val = val | ||
| self.next = next | ||
|
|
||
|
|
||
| class Solution: | ||
| def reorderList(self, head: ListNode | None) -> None: | ||
| if head is None: | ||
| return | ||
|
|
||
| def find_middle(head: ListNode | None) -> ListNode | None: | ||
| # when the number of nodes is odd, the middle node is the left half of the list | ||
| # when the number of nodes is even, the middle node is the right half of the list | ||
| slow = head | ||
| fast = head | ||
| while fast is not None and fast.next is not None: | ||
| slow = slow.next | ||
| fast = fast.next.next | ||
| return slow | ||
|
|
||
| middle = find_middle(head) | ||
| latter = middle.next | ||
| middle.next = None | ||
|
|
||
| def reverse_list(head: ListNode | None) -> ListNode | None: | ||
| node = None | ||
| while head is not None: | ||
| next_head = head.next | ||
| head.next = node | ||
| node = head | ||
| head = next_head | ||
| return node | ||
|
|
||
| reversed_latter = reverse_list(latter) | ||
|
|
||
| def interleave(former: ListNode | None, latter: ListNode | None) -> None: | ||
| while former is not None and latter is not None: | ||
| next_former = former.next | ||
| next_latter = latter.next | ||
| former.next = latter | ||
| latter.next = next_former | ||
| former = next_former | ||
| latter = next_latter | ||
|
|
||
| interleave(head, reversed_latter) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Definition for singly-linked list. | ||
| class ListNode: | ||
| def __init__(self, val=0, next=None): | ||
| self.val = val | ||
| self.next = next | ||
|
|
||
|
|
||
| class Solution: | ||
| def reorderList(self, head: ListNode | None) -> None: | ||
| if head is None: | ||
| return | ||
|
|
||
| def find_middle(head): | ||
| slow = head | ||
| fast = head | ||
| while fast is not None and fast.next is not None: | ||
| slow = slow.next | ||
| fast = fast.next.next | ||
| return slow | ||
|
|
||
| middle = find_middle(head) | ||
| latter = middle.next | ||
| middle.next = None | ||
|
|
||
| def reverse_list(head): | ||
| node = None | ||
| while head is not None: | ||
| next_head = head.next | ||
| head.next = node | ||
| node = head | ||
| head = next_head | ||
| return node | ||
|
|
||
| reversed_latter = reverse_list(latter) | ||
|
|
||
| def merge_lists(former, latter): | ||
| while former is not None and latter is not None: | ||
| next_former = former.next | ||
| next_latter = latter.next | ||
| former.next = latter | ||
| latter.next = next_former | ||
| former = next_former | ||
| latter = next_latter | ||
|
|
||
| merge_lists(head, reversed_latter) |
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.
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.
こちらのコメントをご参照ください。
ksaito0629/leetcode_arai60#1 (comment)