Skip to content

[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 2 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions remove-nth-node-from-end-of-list/donghyeon95.java
Copy link
Contributor

Choose a reason for hiding this comment

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

해당 문제는 LinkedList의 특성을 활용하는 문제로 Discode 리트코드-채팅 채널에 자세히 올려두었으니 참고 부탁드립니다!

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;
}
}

60 changes: 60 additions & 0 deletions same-tree/donghyeon95.java
Copy link
Contributor

Choose a reason for hiding this comment

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

DFS로 푸는 방법도 있지만 Queue 를 활용하여 멋지게 풀이해주신것 같습니다 :)

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();

}
}