-
-
Notifications
You must be signed in to change notification settings - Fork 195
[moonhyeok] Week 11 #1031
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
[moonhyeok] Week 11 #1031
Conversation
#287 은 어떻게 풀어야할지 생각이 안나서 다음주 문제풀 때 해결해보겠습니다. |
/** | ||
* Source: https://www.lintcode.com/problem/178/ | ||
* Solution: 유효한 트리인지 순회하면서 확인하면 되기에 BFS로 구현 | ||
* 시간 복잡도: O(V + E) - 노드와 간선에 한번은 방문 | ||
* 공간 복잡도: O(V + E) - 인접리스트 만큼의 공간 필요 | ||
*/ |
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.
주석을 자세히 적어주셔서, 익숙하지 않은 언어인데도 알고리즘을 이해하기 수월했습니다!
function reorderList(head: ListNode | null): void { | ||
if (!head || !head.next) return; | ||
|
||
// 1. 모든 노드를 배열에 저장 | ||
const nodes: ListNode[] = []; | ||
let current: ListNode | null = head; | ||
while (current) { | ||
nodes.push(current); | ||
current = current.next; | ||
} | ||
|
||
// 2. 배열의 양끝에서 시작하여 리스트 재구성 | ||
let left = 0; | ||
let right = nodes.length - 1; | ||
|
||
while (left < right) { | ||
// 현재 왼쪽 노드의 다음을 저장 | ||
nodes[left].next = nodes[right]; | ||
left++; | ||
|
||
if (left === right) break; | ||
|
||
// 현재 오른쪽 노드를 다음 왼쪽 노드에 연결 | ||
nodes[right].next = nodes[left]; | ||
right--; | ||
} | ||
|
||
// 마지막 노드의 next를 null로 설정 | ||
nodes[left].next = null; | ||
} |
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.
공간복잡도 개선을 위해 slow/fast 포인터를 사용한 방법도 도전해보시면 좋을 것 같습니다!
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.
@mike2ox 님 이번 주도 고생하셨습니다! 주석을 꼼꼼하게 적어주셔서 코드 리뷰가 수월했고, 공부도 많이 되었습니다. 덕분에 "Maximum depth of binary tree"를 반복문으로 접근해볼 수 있었습니다 😄
답안 제출 문제
체크 리스트
In Review
로 설정해주세요.