Skip to content
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
28 changes: 28 additions & 0 deletions reverse-linked-list/reeseo3o.js
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Reverse Linked List
  • 설명: 이 코드는 연결 리스트를 뒤집는 문제로, 명시적 패턴 목록에 없지만 일반적으로 'Reverse Linked List'로 분류됩니다. 주어진 패턴 목록에 포함되지 않아 빈 배열로 처리합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Time Complexity: O(n) - 노드를 한 번만 순회
// Space Complexity: O(1) - 변수 3개(prev, current, next)만 사용
const reverseList = (head) => {
let prev = null;
let current = head;

while (current !== null) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}

return prev;
};

// Time Complexity: O(n) - 노드 수만큼 재귀 호출
// Space Complexity: O(n) - 콜 스택이 노드 수만큼 쌓임
const reverseListRecursive = (head) => {
if (head === null || head.next === null) return head;

const newHead = reverseListRecursive(head.next);

head.next.next = head;
head.next = null;

return newHead;
};
Loading