はじめに
LeetCodeの問題を解答します。
なるべく、問題の和訳と詳細なコメントを書いています。
余裕があれば、複数のアプローチの解答と、実際の面接を想定して英語での解法やコメントを書いています。
様々なカテゴリの問題をランダムにあたる方法も良いですが、
二分探索、連結リストなど、テーマを絞って集中的に解いた方が練習しやすい方は以下の記事が有用です。
テーマごとに問題を分類しました。
LeeetCodeの問題をアルゴリズムとデータ構造による分類しました。
また、LeetCodeではAtcoderと違ってクラスと関数を定義して解答します。
LeetCodeに特有の内容など、知っておくと役に立つかもしれないことをまとめています。
はじめてLeetCodeに触れる方はこちらの記事も役に立つと思います。
ポイント
-
- 循環する連結リスト
- two pointer
この記事で得られること
-
- 連結リストの循環を判定できます。
- two pointerの使い方の練習ができます。
詳細
問題
原文
Given the
head
of a linked list, return the node where the cycle begins. If there is no cycle, returnnull
.There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the
next
pointer. Internally,pos
is used to denote the index of the node that tail’snext
pointer is connected to (0-indexed). It is-1
if there is no cycle. Note thatpos
is not passed as a parameter.Do not modify the linked list.
Example 1:
123 Input: head = [3,2,0,-4], pos = 1Output: tail connects to node index 1Explanation: There is a cycle in the linked list, where tail connects to the second node.Example 2:
123 Input: head = [1,2], pos = 0Output: tail connects to node index 0Explanation: There is a cycle in the linked list, where tail connects to the first node.Example 3:
123 Input: head = [1], pos = -1Output: no cycleExplanation: There is no cycle in the linked list.
Constraints:
- The number of the nodes in the list is in the range
[0, 104]
.-105 <= Node.val <= 105
pos
is-1
or a valid index in the linked-list.
内容(和訳)
※正しくない可能性があります。
解答
解答1:Python , two pointer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: slow = fast = head isCycle = False while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: isCycle = True break if isCycle is False: return None else: slow = head while slow != fast: slow = slow.next fast = fast.next return slow |
two pointerを使います。
1つ進むslowと2つ進むfastポインタを無限に繰り返します。
循環があれば、いつかslowとfastは同じノードを指すので、その時点で無限ループを抜けます。
循環がない場合は、fastが連結リストの末尾(None)にたどり着きます。
循環がある場合は、slowとfastが一致したノードと、連結リストの先頭であるheadから同時に1つずつ進むことで、それらが一致したときに循環の開始ノードとなるので、そのノードを返します。(これは自分では気づけないと思いました。)
終わりに
補足・参考・感想
問題を分類しました。テーマごとに集中して問題を解くことができます。
LeeetCodeの問題をアルゴリズムとデータ構造による分類
LeetCodeに特有の内容など、知っておくと役に立つかもしれないことをまとめました。
疑問が解決した方は他の問題もどうぞ
次:102. Binary Tree Level Order Traversal
疑問が解決しない方はこちらへ
・連結リストを作るためのListNodeクラスがありますが、クラスに初めて触れる方はこちらへどうぞ。
・連結リストについてまとめたページもあります。
コメント