본문 바로가기

열정가득한 개발자의 이야기/Javascript Algorithm

leetcode 141 Linked List Cycle (JS)

반응형

Hi! Long time no see.
Today, I studied linked lists. I think they’re kind of difficult for me, but I’m trying to organize what I’ve learned.

Before I explain the problem, I want to say — this problem actually reminds me of one of my life mottos.
It’s a Korean saying: “People who are meant to meet will meet someday.”
It means that if someone is meant to be a part of my life, I’ll eventually meet them — no matter what.
And this problem is just like that.

 

To summarize: there are two pointers.
One moves one step at a time, and the other moves two steps.
At the beginning, they both start at the same point.
Then they move at different speeds, but in the same direction.
If there is a cycle in the linked list, they will eventually meet — just like destiny.
But if there’s no cycle, they’ll never meet, and the function will return false.

 

 

 

 

반응형