Files
orientada-a-objectos/linked-lists/linked_list.py
T
innin-jam 4a5c0c03fd Änderungen von letzter Stunde
Ich konnte nicht während des Unterrichts pushen.
Dieser Commit enthält die Änderungen,
von Nextcloud kopiert
2026-09-27 18:23:48 +02:00

47 lines
1.0 KiB
Python

from typing import Self
class Node:
def __init__(self, data: int):
self.data: int = data
self.next: Self | None = None
class LinkedList:
def __init__(self):
self.head: Node | None = None
# Definition der "append" Methode ich mag push und pop lieber >:)
def push(self, data: int):
if self.head is None:
self.head = Node(data)
return
node = self.head
assert node is not None
while True:
if node.next is None:
node.next = Node(data)
return
node = node.next
def pop(self) -> int | None:
if self.head is None:
self.head = None
return None
node = self.head
assert node is not None
while True:
if node.next is None:
return node.data
node = node.next
def print(self):
node = self.head
while node is not None:
print(node.data)
node = node.next