Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions 03 보너스 - 배운 것 응용하기/03-linked-list.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,20 @@ def push(self, item: T) -> None:
cur_node = cur_node.pointer
cur_node.pointer = new_node

def pop(self) -> T:
def pop(self) -> Optional[T]:
if self.head is None:
raise ValueError("stack is empty")
cur_node = self.head
raise ValueError("Stack is empty")
cur_node: Node[T] = self.head
if cur_node.pointer is None:
self.head = None
return cur_node.item
while cur_node.pointer.pointer is not None:
cur_node = cur_node.pointer
result = cur_node.pointer
cur_node.pointer = None
return result.item
while cur_node.pointer is not None:
if cur_node.pointer.pointer is not None:
cur_node = cur_node.pointer
continue
result = cur_node.pointer
cur_node.pointer = None
return result.item if result is not None else None


class Queue(Generic[T], LinkedList[T]):
Expand Down