Siklus Daftar Tertaut
Panduan terperinci dan implementasi Python untuk masalah 'Siklus Daftar Tertaut'.
1. Belajar
Masalah 'Siklus Linked List' merupakan tantangan utama di bagian Linked List.
Implementasi ini berfokus pada logika tingkat mudah di Python.
Kami memprioritaskan akurasi teknis dan keterbacaan kode dalam solusi yang kami berikan.
2. Real-World Applications
3. Visual Intuition
Memvisualisasikan aliran logika untuk Siklus Daftar Tertaut.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Bacalah pernyataan masalah untuk Siklus Daftar Tertaut dengan cermat.
2. Formulate brute force
Draf solusi berulang sederhana.
3. Identify inefficiency
Cari perhitungan yang berlebihan.
4. Optimize search path
Gunakan hashing atau pengurutan untuk mempercepat proses.
5. Final Implementation
Bersihkan kode untuk standar produksi.
Pernyataan Masalah
Mengingat head, kepala dari daftar tertaut, tentukan apakah daftar tertaut memiliki siklus di dalamnya.
Ada siklus dalam daftar tertaut jika ada beberapa node dalam daftar yang dapat dijangkau kembali dengan terus mengikuti penunjuk berikutnya. Secara internal, pos digunakan untuk menunjukkan indeks dari node yang terhubung dengan penunjuk ekor berikutnya. Perhatikan bahwa pos tidak diteruskan sebagai parameter.
Mengembalikan nilai benar jika ada siklus dalam daftar tertaut. Jika tidak, kembalikan salah.
Input diberikan sebagai daftar nilai dan pos bilangan bulat (indeks tempat ekor terhubung, -1 jika tidak ada siklus). Menerapkan fungsi hasCycle(head: list, pos: int) -> bool.
- •The number of nodes in the list is in the range [0, 10000]
- •-100000 <= Node.val <= 100000
- •pos is -1 or a valid index in the linked list
Contoh
[3,2,0,-4], 1
True
There is a cycle: the tail node (-4) connects back to the node at index 1 (value 2).
[1,2], 0
True
There is a cycle: the tail node (2) connects back to the node at index 0 (value 1).
[1], -1
False
There is no cycle in the list.
Need a Hint?
Edge Cases to Watch
- Struktur masukan kosong
- Masukan elemen tunggal
- Batasan angka yang besar
Siap Memecahkannya?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Wawasan & Variasi Wawancara
Perincian Analisis Kompleksitas
Mengapa Waktu: Directly evaluates all possibilities.
Mengapa Luar Angkasa: Uses standard local memory.
Mengapa Waktu: Optimized paths reduce total operations.
Mengapa Luar Angkasa: May trade memory for speed.
Solusi yang Dioptimalkan Kode Python
Solusi yang Dioptimalkan Kode Python
def has_cycle_opt(head, pos=None):
if isinstance(head, list):
if pos is None or pos == -1:
h = build_linked_list(head)
else:
h = build_linked_list(head)
curr = h
cycle_node = None
idx = 0
while curr.next:
if idx == pos:
cycle_node = curr
curr = curr.next
idx += 1
if idx == pos:
cycle_node = curr
curr.next = cycle_node
return has_cycle_opt_helper(h)
return has_cycle_opt_helper(head)
def has_cycle_opt_helper(head: ListNode) -> bool:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return FalseKode Brute Force (Spoiler Dilindungi)
Kode Brute Force (Spoiler Dilindungi)
def has_cycle_brute(head, pos=None):
if isinstance(head, list):
# Rebuild linked list with a cycle
if pos is None or pos == -1:
h = build_linked_list(head)
else:
h = build_linked_list(head)
curr = h
cycle_node = None
idx = 0
while curr.next:
if idx == pos:
cycle_node = curr
curr = curr.next
idx += 1
if idx == pos:
cycle_node = curr
curr.next = cycle_node
return has_cycle_brute_helper(h)
return has_cycle_brute_helper(head)
def has_cycle_brute_helper(head: ListNode) -> bool:
seen = set()
curr = head
while curr:
if curr in seen:
return True
seen.add(curr)
curr = curr.next
return FalseAlgorithm Pattern Checklist
When dealing with Linked List data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
Key Revision Notes
Properti masalah Daftar Tertaut Standar berlaku.
Pertanyaan Terkait
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
Sumber Daya Python yang Direkomendasikan
Perluas pengetahuan Anda dengan tutorial interaktif terkait, lembar contekan, dan perbandingan kode.
Daftar Python
Pelajari segala sesuatu tentang daftar Python. Temukan cara membuat, mengiris, memodifikasi, dan melakukan iterasi melalui array dengan Python secara asli.
Cara Mengurutkan Daftar dengan Python
Pelajari cara mengurutkan daftar dengan Python menggunakan metode sortir() dan fungsi sortir(). Temukan contoh pengurutan kunci khusus dan urutan terbalik.
Lembar
Panduan referensi cepat untuk operasi daftar Python. Kuasai penambahan, penyisipan, penghapusan, pengurutan, dan pengirisan elemen.
Python vs JavaScript: Bahasa Pemrograman Mana yang Terbaik?
Perbandingan komprehensif antara Python dan JavaScript. Jelajahi perbedaan sintaksis, kinerja, kasus penggunaan (backend vs frontend), dan contoh pengkodean.