카테고리 없음

[Python]연결리스트 -> 리스트, 리스트-> 연결리스트

갬발장 2023. 7. 16. 20:20

 

1. 연결리스트의 기본 구조

 

class node():
    def __init__(self,data):
        self.data= data
        self.next= None

 

 

2. 리스트를 연결리스트로 ->

 

def listtoliked(arr):
        if len(arr)>0:
            head = node()
            node.data = arr[0]
            current = head
            
            for i in range(1,len(arr)-1):
                new_node = node()
                node.data= arr[i]
                current.next = new_node
                current = current.next

 

3.연결리스트를 리스트로

 

def linkedtolist(linked):
        result =[ ]#연결 리스트를 넣어 줄 리스트
        if linked is None:
            return
        
        while linked is not None:
            result.append(linked.data)
            linked = linked.next