/
erddrsf
/
Pyhse
Обзор
Документация
Войти
/
erddrsf
/
Pyhse
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
linked_list.py
154 строки
5 KB
erddrsf
Примеры моего кода
01 май 2026, 23:35
Верифицирован
01 май 2026, 23:35
b05b859
Код
Авторство
О чём код?
class Linked_Node(): def __init__(self, value=0, next=None): self.value = value self.next = next class Linked_list(): def __init__(self): self.head = None self.tail = None def len_list(self): count = 0 node = self.head while node is not None: count += 1 node = node.next return count def find_middle(self): if self.head is None: print("Список пустой") return None slow = self.head fast = self.head while fast is not None and fast.next is not None: slow = slow.next fast = fast.next.next return slow def add_first(self, node): if not isinstance(node, Linked_Node): print("Ошибка: добавляемый элемент не является Linked_Node") return if self.head is None and self.tail is None: self.head = node self.tail = node else: node.next = self.head self.head = node def add_at(self, node_insert, idx): if not isinstance(node_insert, Linked_Node): print("Ошибка: добавляемый элемент не является Linked_Node") return length = self.len_list() if idx < 0 or idx > length: print("Ошибка: некорректный индекс") return if idx == 0: self.add_first(node_insert) return node = self.head for i in range(idx - 1): node = node.next node_insert.next = node.next node.next = node_insert if node_insert.next is None: self.tail = node_insert def add_last(self, node): if not isinstance(node, Linked_Node): print("Ошибка: добавляемый элемент не является Linked_Node") return if self.head is None and self.tail is None: self.head = node self.tail = node else: self.tail.next = node self.tail = node def delete_first(self): if self.head is None: print("Ошибка: список уже пуст") return if self.head.next is None: self.head = None self.tail = None else: self.head = self.head.next def delete_at(self, idx): length = self.len_list() if self.head is None: print("Ошибка: список пуст") return if idx < 0 or idx >= length: print("Ошибка: некорректный индекс") return if idx == 0: self.delete_first() return node = self.head for i in range(idx - 1): node = node.next if node.next is None: print("Ошибка: индекс вне диапазона") return node.next = node.next.next if node.next is None: self.tail = node def delete_last(self): if self.head is None: print("Ошибка: список пуст") return if self.head.next is None: self.head = None self.tail = None return node = self.head while node.next != self.tail: node = node.next node.next = None self.tail = node def print_all_list(self): if self.head is None: print("пустой список") return node = self.head while node is not None: print(node.value) node = node.next def detect_cycle_and_length(self): slow = self.head fast = self.head while fast is not None and fast.next is not None: slow = slow.next fast = fast.next.next if slow == fast: cycle_length = 1 current = slow while current.next != slow: current = current.next cycle_length += 1 return cycle_length return 0 def find_kth_from_end(self, k): if k < 1: print("Ошибка: k должно быть положительным числом") return None first = self.head second = self.head for _ in range(k): if first is None: print("Ошибка: k больше длины списка") return None first = first.next while first is not None: first = first.next second = second.next return second