File tree Expand file tree Collapse file tree 2 files changed +82
-0
lines changed
remove-nth-node-from-end-of-list Expand file tree Collapse file tree 2 files changed +82
-0
lines changed Original file line number Diff line number Diff line change 1+ public class ListNode {
2+ int val ;
3+ ListNode next ;
4+
5+ ListNode () {
6+ }
7+
8+ ListNode (int val ) {
9+ this .val = val ;
10+ }
11+
12+ ListNode (int val , ListNode next ) {
13+ this .val = val ;
14+ this .next = next ;
15+ }
16+ }
17+
18+ class Solution {
19+ public ListNode removeNthFromEnd (ListNode head , int n ) {
20+ int length = 0 ;
21+ ListNode cur = head ;
22+
23+ while (cur != null ) {
24+ cur = cur .next ;
25+ length ++;
26+ }
27+
28+ int start = 1 ;
29+ int targetIdx = length - n + 1 ;
30+ ListNode result = new ListNode (-1 );
31+ ListNode c = result ;
32+
33+ while (head != null ) {
34+ if (targetIdx == start ) {
35+ head = head .next ;
36+ start ++;
37+ continue ;
38+ }
39+
40+ c .next = new ListNode (head .val );
41+ c = c .next ;
42+ start ++;
43+ head = head .next ;
44+ }
45+
46+ return result .next ;
47+ }
48+ }
Original file line number Diff line number Diff line change 1+ class TreeNode {
2+ int val ;
3+ TreeNode left ;
4+ TreeNode right ;
5+
6+ TreeNode () {
7+ }
8+
9+ TreeNode (int val ) {
10+ this .val = val ;
11+ }
12+
13+ TreeNode (int val , TreeNode left , TreeNode right ) {
14+ this .val = val ;
15+ this .left = left ;
16+ this .right = right ;
17+ }
18+ }
19+
20+ class Solution {
21+ public boolean isSameTree (TreeNode p , TreeNode q ) {
22+
23+ if (p == null && q == null ) {
24+ return true ;
25+ }
26+
27+ if (p == null || q == null || q .val != p .val ) {
28+ return false ;
29+ }
30+
31+ // 특정 깊이의 Node 가 같다면 null을 만날때 까지 탐색하고, 같지 않다면 바로 false를 반환.
32+ return isSameTree (p .left , q .left ) && isSameTree (p .right , q .right );
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments