Skip to content
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

[hodoli] Week 7 #932

Merged
merged 1 commit into from
Jan 25, 2025
Merged
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
31 changes: 31 additions & 0 deletions reverse-linked-list/yeeZinu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* 문제: 리스트 뒤집기
*
* 시간 복잡도: O(n)
* 공간 복잡도: O(n)
Comment on lines +11 to +12
Copy link
Contributor

Choose a reason for hiding this comment

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

저도 같은 방식으로 문제를 풀었는데, 저는 세 개의 포인터만 추가 공간으로 사용하기 때문에 공간 복잡도가 O(1)이라고 생각했습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

앗 그러네요
포인터 3개만 사용되니 O(1)이 맞는것 같습니다!

*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
// 노드 값을 선언
let node = null;

// head가 없을 때 까지 반복
while(head) {
const temp = head.next; // head 의 다음 값을 temp에 저장
head.next = node; // 기존 head의 다음값을 node에 저장
node = head; // 현재 node의 값을 head에 저장
head = temp; // 현재 head값에 temp 저장
}

return node; // node 출력
};
Loading