코딩테스트준비/Python

[Python] Linked-List 구현하기

zyari 2023. 1. 28. 12:34
반응형

1. Linked-List 클래스 선언

class Node:
	def __init__(self, val=0, next=None):
	self.val = val
	self.next = next

 

2. Linked-List 객체 선언

list = Node()

 

3. Linked-List length 측정

while list.next != None:
	list = list.next
    	len+=1

 

4. 특정 길이(loop)만큼 Linked-List에 새로운 원소 추가하기

for i in range(loop):
	#빈 Node 한개 생성
	buf = Node() 
    	buf.value = 0
        
        #채운 노드(buf)를 현재 가리키고 있는 노드의 다음으로 가리키기
	current.next = buf
    
    	#현재 가리키고 있는 노드를 다음 노드로 바꾸기
	current = current.next
반응형

'코딩테스트준비 > Python' 카테고리의 다른 글

[Python] 함수 및 이것저것 아카이브  (0) 2023.01.28