-
-
Notifications
You must be signed in to change notification settings - Fork 195
[donghyeon95] Week 12 #1065
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
Merged
Merged
[donghyeon95] Week 12 #1065
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,37 @@ | ||
import java.util.ArrayDeque; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Queue; | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
class Solution { | ||
public ListNode removeNthFromEnd(ListNode head, int n) { | ||
ArrayList<ListNode> list = new ArrayList<>(); | ||
ListNode nhead = head; | ||
|
||
while (nhead != null) { | ||
list.add(nhead); | ||
nhead = nhead.next; | ||
} | ||
|
||
int size = list.size(); | ||
|
||
if (size == n) { | ||
return head.next; | ||
} | ||
|
||
list.get(size - n - 1).next = list.get(size - n).next; | ||
|
||
return 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. DFS로 푸는 방법도 있지만 Queue 를 활용하여 멋지게 풀이해주신것 같습니다 :) |
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,60 @@ | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
public boolean isSameTree(TreeNode p, TreeNode q) { | ||
// 트리 순회를 한다 | ||
// 현재 순회한 값이 다르다면 return false | ||
if (p==null && q==null) return true; | ||
if (p==null && q!=null) return false; | ||
if (p!=null && q==null) return false; | ||
|
||
Queue<TreeNode> pQue = new LinkedList<>(); | ||
pQue.add(p); | ||
Queue<TreeNode> qQue = new LinkedList<>(); | ||
qQue.add(q); | ||
|
||
while (!pQue.isEmpty() && !qQue.isEmpty()) { | ||
TreeNode pNode = pQue.poll(); | ||
TreeNode qNode = qQue.poll(); | ||
|
||
if (pNode.val != qNode.val) return false; | ||
if (pNode.left!=null && qNode.left!=null) { | ||
pQue.add(pNode.left); | ||
qQue.add(qNode.left); | ||
} else if (pNode.left==null && qNode.left!=null) { | ||
return false; | ||
} else if (pNode.left!=null && qNode.left==null) { | ||
return false; | ||
} | ||
|
||
if (pNode.right!=null && qNode.right!=null) { | ||
pQue.add(pNode.right); | ||
qQue.add(qNode.right); | ||
} else if (pNode.right==null && qNode.right!=null) { | ||
return false; | ||
} else if (pNode.right!=null && qNode.right==null) { | ||
return false; | ||
} | ||
} | ||
|
||
return pQue.isEmpty() && qQue.isEmpty(); | ||
|
||
} | ||
} | ||
|
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.
해당 문제는 LinkedList의 특성을 활용하는 문제로 Discode
리트코드-채팅
채널에 자세히 올려두었으니 참고 부탁드립니다!