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
47 changes: 47 additions & 0 deletions linked-list-cycle/robinyoon-dev.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set
  • 설명: 이 코드는 노드 방문 여부를 Hash Set에 저장하여 순회하며 순환 여부를 판단하는 방식으로, 해시 맵 또는 해시 셋을 사용하는 패턴에 속합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function (head) {

const visited = new Set();
let result = false;

while (head) {

let next = head.next;

if (next == null) {
result = false;
break;
}

let isExist = findElementInDp(head);

if (!isExist) {
visited.add(head);
head = next;
} else {
result = true;
break;
}
}

return result;

function findElementInDp(head) {
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.

findElementInDp 함수 내용 부분은 return visited.has(head) 로 간소화할 수 있을 것 같아요!

if (visited.has(head)) {
return true;
} else {
return false;
}
}
};
Loading