기술(Tech, IT)/파이썬(Python)

[Python] 링크드 리스트 구현 - 1 (Singly Linked List)

Daniel803 2023. 10. 14. 00:12

Singly Linked List 구현은 매우 간단하고, 자주 사용하게 되므로 알아두는 것이 좋다.

 

# Singly Linked List class 정의
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None
        
# '0'이라는 데이터를 담은 head와 cursor 선언
cur = head = Node(0)

# '1'이라는 데이터를 담은 새로운 Node 선언해 head의 다음으로 연결
new = Node(1)
cur.next = new
cur = new

# head부터 출력하면 0, 1을 출력으로 확인 가능
test = head
while test:
	print(test.val)
    test = test.next